Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Application exits when Main form closes

I'm writing a multi form application, and I'd like to be able to close Form1 with out the application closing.
Right now i'm using this.Hide to hide the form. Is there a way to set it so i can close any form, or the application should only exist when all the forms are closed?

I think i remember seeing something like that at one point, but that might not have been visual studio and c#.

like image 201
WolfyD Avatar asked Jul 30 '13 17:07

WolfyD


2 Answers

In your Program.cs file you have a line like Application.Run(new Form1()). Replace it with

var main_form = new Form1();
main_form.Show();
Application.Run();

Also you need to close application explicitly by call Application.Exit() where you want.

like image 140
dmay Avatar answered Sep 28 '22 01:09

dmay


One strategy is to do something like the following:

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    if (Application.OpenForms.Count == 0)
        Application.Exit();
    }
}

If you place this on all of your forms, when the last one closes, the application will exit.

like image 31
smbogan Avatar answered Sep 28 '22 01:09

smbogan