Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey I have the problem that when I close Form1 (my opening form) all the other forms close down.

How do I set it so that Form2 is the form that when closed, closes all of them down, however I still want Form 1 to run at the begining of the project.
I'm using visual studio 2010

Thanks
Posted
Updated 12-Jan-11 8:53am
v3

This might help, but I'm not guaranteeing anything because your question is poorly worded.

Multiple Subsequent "Main" Forms in C# Apps[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-11 15:00pm    
All right, I did not know someone already done this crazy thing -- my 5.
You cannot do it in a simple way, because -- look at your entry point ("main"): Application Run is called with your form; that defines which form is main. Its instance is returned as the static read-only (important!) property Application.MainForm. There is no API to switch the main form -- for a good reason!

My best believe: you should not do this, because it will confuse your user very well.

Nevertheless, if you're as crazy as I am and want to work around this limitation, you can do this! You should

1) Close your main form and go out of application loop.

2) Now, you need to call Application.Run again with different parameter (your different form). You should understand that it may require your different form to be closed and re-opened, then you may need to store (persist it) and completely restore its state. You need to write this code in your main method.

3) You may want to make this call conditional, otherwise you will not see

4) You may look Application.Run within your main, switching main form case to case.

5) You should provide an option to allow the user to "close application completely", because, considering item (4), you may run it forever. This can be done in menu item "Exit", for example, as closing a form is no longer supports complete termination of the application. You will need a static condition data field (only because your main is static).

I did it, out of curiosity.

Now, you need either to say "I don't need it" and accept my answer or write a code using my plan. Optionally, I would gladly write this code, if you convince me that this effort is welcome (better after you show some of your effort). In all cases, I agree to answer reasonable follow-up questions.

Again, I do not recommend doing it for real-life application!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jan-11 15:01pm    
All right, John's reference should work. It was designed to a special case, I don't know if this is what you need. But I explained the idea.
WurmInfinity 12-Jan-11 15:19pm    
My friend has just suggested that in the Program.Cs file there is a peice of script called Application.Run(new Form1()); this shows wich form is run first however thats great but I now that would mean my Form2 needs to be my main form and I have no idea how to switch the two.
Sergey Alexandrovich Kryukov 12-Jan-11 15:43pm    
I just answered. Please indicate the following: 1) do you really need to switch from main form to main form during same application run-time (I warned against it!); 2) doesn't the John's answer provide you with what you want; 3) provide a little bit of background: why? what's the ultimate purpose? 3) indicate what exactly you did not understand in my explanation?

Based on your reply, chances are, I'll help you with some code.
Possibly something as simple as showing Form2 from with Form1's initialization, then hiding Form1 until Form2 closes?

Here's a dirty sample:
MIDL
public partial class Form1 : Form
{
    public Form2 frm2 = new Form2();
    public Form1()
    {
        InitializeComponent();
        Hide();
        frm2.ShowDialog();
        Show();
    }
}


Form1 doesn't get a chance to be seen until Form2 closes.
 
Share this answer
 
Comments
WurmInfinity 12-Jan-11 15:22pm    
Yeah I had this.Hide before, However the problem is Im reading and writing XML files and when Form1 is still open but hidden its using the file and the new form cannot use it.
GenJerDan 12-Jan-11 15:24pm    
Ah. Oh, well. Didn't know you had something locked by Form1.

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