Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
I have two forms ie form1 and form2. Now I have a button on form1. On click of a button i want to close form1 and show form2. The code I have written is like this:
form2 f2=new form2()
f2.show();
this.close()
But this.close closes the entire application.
 
Could someone please help me?
Thanks in advance.
Posted 12-Aug-10 5:08am
Edited 12-Aug-10 5:20am
v2
Comments
Toli Cuturicu at 12-Aug-10 10:21am
   
Do not ever again lock a question for editing. It is extremely rude. (to E.F. Nijboer)
E.F. Nijboer at 12-Aug-10 10:32am
   
When improving the question it gets locked but you still would be able to answer it though. It isn't rude, it's simply to prevent collisions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you look in Program.cs, you'll see that Form1 is passed in to Application.Run.
 
When you close Form1, the framework assumes you're exiting the program.
 
You can hide Form1, then show Form2, or you can create a background manager and use that in Application.Run to control which form is being used. This will feel a bit hackish unless you find/work out a decent pattern for knowing which forms are open and when you're supposed to close. You will need to create and manage an ApplicationContext that shuts itself down when you have no more forms open. It should also be responsible for creating the forms.
 
Cheers.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is because you defined Form1 as your main form. You could hide it instead (form1.visible = false;) of closing it.
 
It is possible to close form1 but you have to run the second form to get the messageloop going again, like this:
Application.Run(new form2());
 
Good luck!
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As form1 is parent and form2 is child form.. so it is not possible to open a child form when a parent form is closed.
 
SPAM link removed.
  Permalink  
v2
Comments
phil.o at 15-Jan-13 3:59am
   
Great topic digging !
markovl at 15-Jan-13 4:44am
   
It was only to post SPAM, so I guess they don't care too much :-)

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100