10

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#.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
WolfyD
  • 865
  • 3
  • 14
  • 29
  • Take a look at `Program.cs`. The form used on the 3rd line is your "root" form, and the program will close when this form closes. Because there is nothing for the program to do after running the form, it closes. What do you want the program to do after closing the form? – gunr2171 Jul 30 '13 at 17:17
  • Possible duplicate http://stackoverflow.com/questions/5599097/close-a-windows-form-without-exiting-the-entire-application?rq=1 – jrbeverly Jul 30 '13 at 17:17

4 Answers4

20

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.

dmay
  • 1,340
  • 8
  • 23
  • 3
    Thank you! It worked, BUT I had to do it like this: `var main_form = new Form1();` `main_form.Show();` `Application.Run();` or else it just started up with no form showing. Thanks again! – WolfyD Jul 30 '13 at 17:40
  • 1
    Yep, forgot about it. Added it to the answer. – dmay Jul 30 '13 at 17:47
7

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.

smbogan
  • 91
  • 1
  • 3
1

A multiform aplication should have a clear EXIT option (either by menu, toolbar), since you can not know that the user wants to close the program when the last window is closed (i supose that the user could go to the File/Open and open new windows)

An aplication that does something automatically that the user did not asked for can used frustration/confusion and spend time reopening the aplication.

Even the user can think that the application somehow crashed since he did not close it.

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Thanks, that is some good to know stuff. I actually make sure to make my applications user friendly, I don'T usually limit their ability to resize windows and stuff like that but this is a linear application and it has a specific flow. When the user clicks the next button I want it to go to the next form and close the previous one. I could hide it, but I prefer not to waste CPU time on an invisible form the user won't ever see again. – WolfyD Jul 30 '13 at 17:28
  • Also the user will get a chance to exit the application on every form. – WolfyD Jul 30 '13 at 17:29
  • @WolfyD if its a single flow, why not implement the typical wizard interface with the BACK, NEXT, CANCEL buttons ? the NEXT buttons is changed to the FINISH when the desired state has been reached – Mauricio Gracia Gutierrez Aug 02 '13 at 13:25
0

Based on your coments it's a single flow kind of application

I suggest that you implement the typical wizard interface on a single form with the BACK, NEXT, CANCEL buttons.

When the desired state has been reached, for example enough information has been gathered , the NEXT button is changed to a FINISH button.

Any time the user presses the CANCEL/FINISH button the window will close

And if you still want multiform you could still have it, in this you could have multiple flows at the same time and just finish or cancel the one that you want.

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Yes, i did think of that, but since every form requires different controls and information it would have been pretty hectic to set up, but i worked it out in such a way that when the user gets on the next form the application keeps working and when the user wants to close the form either using the [x], the task manager or the quit button it closes the whole application, so there's no confusion. – WolfyD Aug 09 '13 at 16:03