How to close Form1 when Form2 displays ??

  • Thread starter Thread starter ematrix
  • Start date Start date
Assuming that Form1 is displaying Form2 then you could have something
similar to the following code.

Form2 f = new Form2();
f.Show();
this.Close();

Be cautioned, however, that if Form1 is the main Form (startup Form) then
closing it will end the application. If this is the case then you may
consider displaying Form2 using its ShowDialog method instead, or you could
hide Form1 instead of closing it.
 
Thanks a lot

But that two methods are not good,can I change Form2 to main form before
closing Form1,thanks
 
AFAIK, no, you cannot. What is the purpose of Form1? Why is it the first one
to be shown but also must close?
 
Thanks a lot
The two methods you considered are not good I think.If open Form2 by
ShowDialog(),form1 is back form2 only;If hide form1 before open form2,form1
is only running after closing form2
 
So Form1 is used to allow the user to log into the application? Is that
right? There are a couple ways to handle this.

(1)
Ensure that Form2 is set to be the startup Form, the one that is passed to
the "Run" method of the Application class, and then display the login Form
from the Load event of Form2 by calling the login Forms ShowDialog method.
You can see an example of this in the TaskVision sample application
(http://windowsforms.net/Applications/application.aspx?PageID=20&tabindex=8)
..

(2)
Use the ApplicationContext class to initially display the login Form, and
then switch over to Form2 when the login Form is closed and the user is
validated. You can see an example of this in relation to a splash screen
here (http://www.codeproject.com/csharp/ApplicationContextSplash.asp).

There may be others ways to accomplish this but there are two methods
outlined above and one of those should fit your requirements.
 
Back
Top