0

I try to switch panels on tabControls on UserControl. Like this

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;
}

but when N exceeds 6, switching panels doesn't work well.

Some panels don't visible ,even if the Button event occurs!

So could you tell me how to switch multi panels.

If possible, could you tell me the smart way to switch panels.

The above code seems to be bad readability.

Yur
  • 11
  • 2
  • 3
  • 1
    `TabControl` has a `SelectedIndex` property to switch to another tab – Alexey S. Larionov Jun 26 '20 at 12:03
  • I want the panels to switch on A page of tabcontrols. – Yur Jun 26 '20 at 12:05
  • You can try replacing all your panels with another `TabControl` (i.e. make another `TabControl` on a page of your `TabControl`). Then disable headings of this new tab control [like it's suggested here](https://stackoverflow.com/questions/1465443/hide-the-tabcontrol-header). Then switch between your panels via the new `TabControl.SelectedIndex` – Alexey S. Larionov Jun 26 '20 at 12:11
  • Try bring the panel to Front. It may not be seen if it is behind another control. – jdweng Jun 26 '20 at 12:43

1 Answers1

0

You could make all of the buttons fire the same handler, then do something like this:

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();
            }
        }
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40