Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai
I have two forms in my app.
The second form is shown a few second after the first form appear using timer.
Now i need the first form to close automatically when the second form appear.
How this can be done?
Posted

You cannot do it during the single run-time of the instance of System.Windows.Forms.Application.
When you call Application.Run, its parameter defined the instance of the System.Windows.Forms.Form which assumes the role of Main Form. All other forms will be non-main. Every time you close the main form, Application is closed. It means, Application.Run exits. Is does not happens with any other form unless you explicitly call Application.Exit.

So, in principle you can use tricky technique inside your application's entry point (normally, this is the Main method):

static void Main() {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Form firstMain = new FirstMain();
   Form secondMain = new SecondMain();

   // inside this call firstMain may or may not show secondMain,
   // close itself and so exit Run method:
   Application.Run(firstMain);

   if (CertainConditionYouMightDesign(firstMain)
       Applicatin.Run(secondMain);
} //Main


This certainly will work but… don't do it!
This will be a pretty big abuse of the intended use of the Forms application.

You can imitate desired effect hiding forms instead of closing. Naturally, you need to preserve a way to exit application explicitly. Every time the form is closed, you need to make sure another form is shown. This technique is the most usual and successful.

This is how to hide a form on attempt to close it:

C#
class SecondMain : Form {

//...

    protected override void OnFormClosing(FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.UserClosing) {
            e.Cancel = true;
            Hide();
        } //if
    } //OnFormClosing

} //class SecondMain


I'm not showing the logic of automatic closing one form and showing another one. Basically, you should create some interface and implement it on each form. This interface should have one or two methods for communications between forms, so the form would tell to each other: "I'm closing, you show yourself and go to top". In the beginning of the forms life cycle, you need to pass the interface reference for the other form to each other. (You could directly pass instances of the forms to each other and have some public methods for such interaction, but this is a violation of isolation principles; delegates is the most safe technique; you should not pass more access then it's really needed.)

This technique is especially convenient thanks to using of partial class declaration. You can create more separate files for the same class and add one more partial declaration devoted to this effect. For more information on form collaboration, see my past answer here How to copy all the items between listboxes in two forms[^]. Consider other solutions and see the discussion.

Now, final advice: do not do it all! Change the general design. Make one and only one main form, fill it with some TabControl and make some tabs. What were you separate windows make a separate tab pages. Simple and civilized. More than one form is not really nice solution.

—SA
 
Share this answer
 
Comments
thatraja 3-Jun-11 1:41am    
Complete answer. Yes, Also I recommend the Tabcontrol.
Sergey Alexandrovich Kryukov 3-Jun-11 1:45am    
Thank you, Raja.
--SA
Show the second form and then hide the first form.

C#
Form2 frm2 = new Form2();
frm2.show();

this.hide()
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 2-Jun-11 23:45pm    
Not so easy! One form is the main one, you cannot just remove it.
Your solution won't work. If you don't trust me, check test it and see all the trouble. I suggest you fix or remove it. (I did not vote.)

Please see my solution.
--SA
Wild-Programmer 3-Jun-11 0:41am    
SA you got emotional, I trust you completely :)

just saw that code as answer in one forum, so I thought i will work. Maybe .hide() will work. I changed it.
Sergey Alexandrovich Kryukov 3-Jun-11 1:52am    
Amit, thank you for your note.

Just one reply: you should not thing that "if you don't trust me" is said emotionally. In the culture of science I'm used to it is considered normal that no fact is taken without doubt and re-checks. I just suggested you test your solutions, in particular, before posting. Mistakes happen in all cases though, everyone is supposed to understand it.

Thank you for understanding.
--SA
Wild-Programmer 5-Jun-11 6:15am    
Sure, I understand and will take care of that next time :)

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