1

I try to show/hide panels in C#, but when I clicked on button1 I wanted to see panel1 but panel2 appeared. And when I cliked on button2, panel2 dissappeared. But when I cliked first on button2, panel2 didn't appear. I don't know what is wrong with my code but here it is:

public Form3()
    {
        InitializeComponent();
    }

    bool show1;
    bool show2;
    private void button1_Click(object sender, EventArgs e)
    {
        if(show1)
        {
            panel1.Visible = false;
            show1 = false;
        }
        else
        {
            panel1.Visible = true;
            show1 = true;
        }
        Application.DoEvents();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (!show2)
        {
            panel2.Visible = true;
            show2 = true;
        }
        else
        {
            panel2.Visible = false;
            show2 = false;
        }
        Application.DoEvents();
    }
mnwsmit
  • 1,198
  • 2
  • 15
  • 31
MariaD
  • 49
  • 1
  • 8
  • Unfortunately your code is not complete and the problem cannot be reproduced using just what you have. Perhaps you have your events wired up backwards though? – lc. Mar 02 '16 at 07:45
  • panel2.Visible = !panel2.Visible and panel1.Visible = !panel1.Visible – Anil Mar 02 '16 at 07:45
  • 2
    You can remove the `Application.DoEvents`. First: Its use is discouraged (see http://stackoverflow.com/questions/5181777/use-of-application-doevents), second: Control goes back to UI thread right after the `Click` method exits anyway, so the update will be done right away. – Thorsten Dittmar Mar 02 '16 at 07:48

4 Answers4

6

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.

Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
  • I try to use this code, but when I start the programme appeared panel2 (I try to make to appeared a blank panel). When I clicked on button1 appeared panel1, but when I clicked on button2, panel2 didn't appeared. – MariaD Mar 02 '16 at 16:56
  • @MariaD that is pretty strange considering the command is very clear, to set the panel2.Visible = true. First, u can debug if the line is really executed when u click the button. Next, is to confirm that ur panel name is corrrct. Other than that, u may require to use VS debugger to check line by line. – Ian Mar 02 '16 at 17:00
  • I review my Form Design and I saw there another blank panel what name was panel2 and in this case my panel2 don't appeared when I clicked on button2, but now is working and thanks for help. – MariaD Mar 02 '16 at 17:18
3

Don't use global variable like show1 and show2 You can do like this instead.

private void button1_Click(object sender, EventArgs e)
{
    panel1.Visible = !panel1.Visible; 
    Application.DoEvents();
}
Kien Chu
  • 4,735
  • 1
  • 17
  • 31
0

No need to use variables and condition. Just add panel1.Visible = false; or panel1.Visible = true; on button click

rjps12
  • 587
  • 6
  • 15
0

You may find the solution but I just want to recommend you to use SuspendLayout() and ResumeLayout() for performance improvement while changing control states

private void button1_Click(object sender, EventArgs e)
{
     this.SuspendLayout();
     panel1.Visible = true;
     panel2.Visible = false;
     this.ResumeLayout();
     //Application.DoEvents();
}

private void button2_Click(object sender, EventArgs e)
{
     this.SuspendLayout();
     panel2.Visible = true;
     panel1.Visible = false;
     this.ResumeLayout();
     //Application.DoEvents();
}
Mujassir Nasir
  • 1,640
  • 4
  • 31
  • 52