Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to close Form1 when Window Form2 will open?
Posted

1 solution

If Form2 is being opened from Form1, then it is probably not a good idea to close it.

If Form1 is the main form for the application, then closing it will end the application, closing Form2 as well.

If what you want to do is open Form2 from Form1 and then hide Form1:
C#
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Show();
Or
C#
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Close();
Which will end the app when Form2 is closed.
 
Share this answer
 
Comments
Janardan Pandey 14-Mar-12 5:40am    
Ok but i want to ask a question again like this

How to close form2 when form3 will open?
OriginalGriff 14-Mar-12 5:59am    
Depends on what you are doing.
If you display Form2 and Form3 from Form1, using Show instead of ShowDialog then you need to keep a class level variable with the Form2 instance within Form1. You will need to handle the FormClosed event to set the instance to null.
You then close that instance (if any) when you open Form3.
If you are opening Form3 from Form2, then you can just Close Form2 when you have issued Show on Form3. Provided Form1 does not close itself when Form2 closes, that is!
Rubaba 14-Mar-12 18:04pm    
5++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900