Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello friend

i want close the current window when i am click on button and open another window.

i am using code on button click which is given below

MIDL
Form1 f = new Form1();
           f.Show();



MIDL
Form2 f2 = new Form2();
           f.Close();


but its not working i am use hide also but its not working can any one help me
Posted

To answer your question:

C#
Form1 f = new Form1();
f.Closed += new ....
f.ShowDialog();


and in the Closed event handler for Form1

C#
Form2 f = new Form2();
f.ShowDialog();


Maybe if you described EXACTLY what you're trying to do, we could give you a better answer.
 
Share this answer
 
on button click of form1 code this to close the current window:

this.Close();


and write this to open the new window:


Form2 f2 = new Form2();
f2.ShowDialog();


hope this helps :)
 
Share this answer
 
Comments
BobJanova 6-Jul-11 9:20am    
5 for answering the direct question asked, however for what I suspect the OP wants, closing the first form will close the application.
Uday P.Singh 6-Jul-11 10:41am    
yes you are right it will close the application, even I don't understand what OP wants, just gave the answer to his specific requirement.
thanks for 5 :)
After your response to Ace_Hardlight, I think this tip/trick will help you:

Multiple Subsequent "Main" Forms in C# Apps[^]
 
Share this answer
 
Try:

//form to open
Form1 f = new Form1();
f.show();


//current form you want to close
this.close();
 
Share this answer
 
Comments
guptaadeepak 6-Jul-11 8:36am    
when i am run application form1 is open when i am click on button on from1 then form2 is open and form1 is close??

using this.close(); all form is close
Ace_Hardlight 7-Jul-11 1:25am    
that's cause form1 is then the parent form. When you close the parent form all child forms are closed aswell. However, if you use this.Close() on form2, it will only close form2 and form1 will still be open. Which form do you want to close and which one do you want open?

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