Don't use flags, because your button behavior will be determined by the states of the flags.
Best is to code it the way you want. If you want each Button
to make the corresponding panel visible while other panel invisible:
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
//Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
panel2.Visible = true;
panel1.Visible = false;
//Application.DoEvents();
}
Or, if you want each button to control the visibility of each panel independently, do this:
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = !panel1.Visible;
//Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
panel2.Visible = !panel2.Visible;
//Application.DoEvents();
}
Lastly, the Application.DoEvents()
can be removed (credit to Thorsten Dittmar) as the control will immediately back to UI thread after the Click
method finishes anyway. Read his comment and the referred link.