• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5017
  • Last Modified:

Hide/Unhide C# form

I am new to C# so this is probably a very easy question.  I can't seem to figure it out though.  I have a form and when a certain button is clicked I want to hide the form and show a second form.  Then when the close button is clicked on the second form I want to unhide the first form.  

Here is my code for the first form showing the second form.
   fclsmynewform frmtest = new fclsmynewform();
   this.Hide();
   frmtest.ShowDialog();

How do I get my form to reappear without creating an instance of a new form?

Thanks
Tim
0
tbailey922
Asked:
tbailey922
2 Solutions
 
BurntSkyCommented:
Well, there are a couple ways you could do it, I suppose.  You could pass a reference to the first form in the second form's constructor and then call Form1.Show() when you want it to reappear.  However, I would suggest creating an EventHandler for the second form's Closing event and do it there, instead.

private void ShowForm2()
{
    Form2 f = new Form2();
    f.Closing += new CancelEventHandler(Form2_Closing);
    this.Hide();
    f.Show();
}

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.Show();
}
0
 
Iamthe1urwarnedCommented:
Tim,

In the button_Click event of Form1, place the following code:
private void button2_Click_1(object sender, System.EventArgs e)
{
   Form2 frm2 = new Form2();
   this.Hide(); //Hides Form1

   frm2.ShowDialog(); //Displays Form2

   this.ShowDialog(); //After the form2 is closed, again displays Form1
}

In Form2, place the following code in the Closing event:

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   this.Hide();
}


Cheers !!
GodsThrone
0
 
tbailey922Author Commented:
Thanks for the help!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now