【问题标题】:Switch multi panels on the UserControl在 UserControl 上切换多面板
【发布时间】:2020-06-26 22:17:37
【问题描述】:

我尝试在 UserControl 上的 tabControls 上切换面板。 像这样

private void button1_Click(object sender, EventArgs e)
{ 
panel1.Visible=true;
panel2.Visible=false;
.....
panelN.Visible=false;
}
    private void button2_Click(object sender, EventArgs e)
{ 
panel1.Visible=false;
panel2.Visible=true;
.....
panelN.Visible=false;
}

    private void buttonN_Click(object sender, EventArgs e)
{ 
panel1.Visible=false;
panel2.Visible=false;
.....
panelN.Visible=true;
}

但是当N超过6时,切换面板就不好用了。

有些面板是不可见的,即使发生了 Button 事件!

那么你能告诉我如何切换多面板吗?

如果可能的话,你能告诉我切换面板的聪明方法吗?

上面的代码似乎可读性不好。

【问题讨论】:

  • TabControl 有一个SelectedIndex 属性可以切换到另一个选项卡
  • 我希望面板打开一个 tabcontrols 页面。
  • 您可以尝试用另一个TabControl 替换所有面板(即在您的TabControl 的页面上创建另一个TabControl)。然后禁用这个新选项卡控件like it's suggested here 的标题。然后通过新的TabControl.SelectedIndex 在您的面板之间切换
  • 尝试将面板置于前面。如果它位于另一个控件后面,则可能看不到它。

标签: c#


【解决方案1】:

您可以让所有按钮触发同一个处理程序,然后执行以下操作:

private void btn_Clicked(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    int numButtons = 6;
    int index = int.Parse(btn.Name.Replace("button", ""));
    for(int i=1; i<=numButtons; i++)
    {
        Control ctl = this.Controls.Find("panel" + i, true).FirstOrDefault();
        if (ctl != null)
        {
            ctl.Visible = (i == index);
            if (i == index)
            {
                ctl.BringToFront();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2018-03-07
    • 2021-06-13
    相关资源
    最近更新 更多