滑动面板-一种有效,其他无效

时间:2018-09-24 13:03:28

标签: c# winforms

我碰到了一堵墙,我不知道如何设法将它塞满。我正在尝试使用C#在我的应用程序上使用多个面板,并且每个面板都从侧面的菜单中滑入和滑出。我写了一个单独的幻灯片课程:

class Slide
{
    Panel pane;
    Button btn;
    bool hidden;
    Timer t;
    const int maxWidth = 315;

    public Slide(Panel p, Button b)
    {
        this.pane = p;
        this.btn = b;
        hidden = true;

        btn.Click += new EventHandler(btnClick);

        t = new Timer();
        t.Interval = 15;
        t.Tick += new EventHandler(timeTick);
    }

    private void timeTick(object sender, EventArgs e)
    {
        if(hidden)
        {
            SlidingPane(+10);
        }
        else
        {
            SlidingPane(-10);
        }
    }

    private void btnClick(object sender, EventArgs e)
    {
        t.Start();
    }

    private void SlidingPane(int i)
    {
        pane.Width += i;

        if(pane.Width >= maxWidth || pane.Width <= 0)
        {
            t.Stop();
            hidden = !hidden;
        }
    }
}

我已经按照以下步骤初始化了面板:

    Slide menuP, calendarP, peopleP, taskP, settingsP;
    public Form1()
    {
        InitializeComponent();
        ButtonColours();
        InitialisePanes();
    }

    private void InitialisePanes()
    {
        menuP = new Slide(menuPane, menuButton);
        calendarP = new Slide(calendarPane, calendarButton);
        peopleP = new Slide(peoplePane, peopleButton);
        taskP = new Slide(taskPane, toDoButton);
        settingsP = new Slide(settingsPane, settingsButton);
    }

这是工作面板的表单设计器代码:

this.menuPane.BackColor = System.Drawing.Color.SlateGray;
this.menuPane.Controls.Add(this.peoplePane);
this.menuPane.Dock = System.Windows.Forms.DockStyle.Left;
this.menuPane.Location = new System.Drawing.Point(67, 0);
this.menuPane.Name = "menuPane";
this.menuPane.Size = new System.Drawing.Size(0, 652);
this.menuPane.TabIndex = 2;

和其他完全一样。例如:

this.peoplePane.BackColor = System.Drawing.Color.SlateGray;
this.peoplePane.Controls.Add(this.calendarPane);
this.peoplePane.Dock = System.Windows.Forms.DockStyle.Left;
this.peoplePane.Location = new System.Drawing.Point(67, 0);
this.peoplePane.Name = "peoplePane";
this.peoplePane.Size = new System.Drawing.Size(0, 652);
this.peoplePane.TabIndex = 2;

我已经启动了我的应用程序,然后单击menuButton,它起作用了。精美地滑入和滑出。我单击其他按钮,.....什么都没发生。

有人可以看到为什么会这样吗?我正在查看的所有内容都告诉我它应该可以工作。

1 个答案:

答案 0 :(得分:0)

要确保所有窗格彼此正确对齐并且(不)嵌套,您可以使用如下代码:

foreach( Control ctl in new[]  { peoplePane, calendarPane, taskPane, settingsPane })
{
    ctl.Parent = menuPane.Parent;
    ctl.Location = menuPane.Location;
}

它假设menuPane在正确的位置,并使所有其他对象坐在顶部而不嵌套。

您发布的代码包含错误的嵌套,并且使用鼠标将面板移动到同一位置也将创建(在这种情况下是不需要的)嵌套。使用键盘进行移动可以避免这种情况,但很乏味。