1

I am in Form1 Designer Window and I have a Button. When I click on the Button, I should navigate from Form1 to Form2. However, Form1 designer window should get closed.

In the below code when I click on the Button, Form2 window will popup. The issue is Form1 will also be there on the computer screen along with Form2. I wish to see only the window to which I would like to navigate, and the other should disappear.

How to correct the below code accordingly?

private void button1_Click(object sender, EventArgs e)    
{    
   Form2 Check = new Form2();
   Check.Show();  
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
GtR
  • 21
  • 5
  • Possible duplicate of [c# open a new form then close the current form?](http://stackoverflow.com/questions/5548746/c-sharp-open-a-new-form-then-close-the-current-form) – Sayse Dec 04 '15 at 08:02

1 Answers1

0

This code hide Form1 when you are showing Form2 and then will close Form1 when you close Form2:

private void button1_Click(object sender, EventArgs e)
{
    Hide();
    Form2 Check = new Form2();
    Check.Closed += (s, args) => this.Close();
    Check.Show();
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Great. Could you explain what is happening here in the line of code: **Check.Closed += (s, args) => this.Close();** – GtR Dec 04 '15 at 08:05
  • @GtR...`Hide()` will hide your main form and `Check.Closed += (s, args) => this.Close();` will close main form after child form is closed. Your main form is still living after you show `Form2` so you should close it after closing `Form2`. – Salah Akbari Dec 04 '15 at 08:07
  • Check.Loaded+= (s, args) => this.Hide(); – David Dec 04 '15 at 08:09