0

我尝试在 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 事件,某些面板也不可见!

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

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

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

4

1 回答 1

0

您可以使所有按钮触发相同的处理程序,然后执行以下操作:

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();
            }
        }
    }
}
于 2020-06-26T14:17:37.407 回答