To close C# Forms Application

12,693

Solution 1

Your first form is set as the startup form. That means whenever it gets closed, your entire application is closed. And conversely, your application does not close until it gets closed. So when you hide the startup form and show the second form, the user closing the second form does not trigger your application closing because they have only closed a secondary, non-modal dialog.

I recommend changing your design so that the startup form is also the main form of your application. No sense trying to work around built-in functionality that can actually be useful. You want the application to quit when the main form is closed, no matter what other child forms are opened.

But the quick-and-dirty solution in your case is to make a call to Application.Exit. That will close all of the currently open forms and quit your application immediately. As I said just above, I don't so much recommend this approach because having to call Application.Exit from every form's FormClosed event handler is a sign that something has gone seriously wrong in your design.

If the single startup form paradigm doesn't work out for you, you should look into taking matters into your own hands and customizing the Main method in your Program.cs source file. See the answers given to this related question for some ideas on how that might work for you.

Solution 2

What you can do is to use the Form's FormClosing event, and add the following code:

Application.Exit();

This will stop the entire application, and close all windows. However, if a background thread is running, the process itself will survive. In this case you can use:

Environment.Exit();

Solution 3

Add a Application.Exit on every forms's Closing event

like this:

Create an closing event handler first

private void Form_ClosingEventhandler()(object sender, CancelEventArgs e)
{
      //Perform any processing if required like saving user settings or cleaning resources
      Application.Exit();
}

then bind this event to any form you create.

//Where you create new form and show it.
Form1 frm= new Form1();
//set other properties
frm.Closing += new EventHandler(Form_ClosingEventhandler);
Form2 frm2= new Form2();
//set other properties
frm2.Closing += new EventHandler(Form_ClosingEventhandler);

Solution 4

Surely you don't want to shut down the entire application after the user adds a phone number? You just need to make sure that your main window becomes visible again. Write that like this:

    private void AddButton_Click(object sender, EventArgs e) {
        var frm = new AddPhoneNumber();
        frm.StartPosition = FormStartPosition.Manual;
        frm.Location = this.Location;
        frm.Size = this.Size;   // optional
        frm.FormClosing += delegate { this.Show(); };
        frm.Show();
        this.Hide();
    }
Share:
12,693
Vinod K
Author by

Vinod K

Updated on June 05, 2022

Comments

  • Vinod K
    Vinod K almost 2 years

    I have 2 forms ...when i start the application..and use the close "X" from the title bar the entire application closes...now when i select an option from the 1st form in my case it is a button "ADD" as its a phonebook application..it goes to the 2nd form as i have used 1stform.hide() and 2ndform.show()...now when i do "X" from the title bar it doesnt shutdown completely as the 1stform is not closed....how to program it in such a way tht any stage the entire application should close

  • João Angelo
    João Angelo over 13 years
    A background thread does not stop the process from exiting, since it's automatically shutdown. You are probably referring to a foreground thread.
  • Øyvind Bråthen
    Øyvind Bråthen over 13 years
    @João Angelo - What I meant here is that if several threads are running, only the GUI thread will be taken down, and the application will seem to have been closed, but the process is still there in the process list. Thanks for pointing out :)
  • Cody Gray
    Cody Gray over 13 years
    @Vinod: I meant the FormClosed event. The documentation is available here, but it's just a regular event exposed by the Form class. You can subscribe to it just like any other event in your application (for example, Button.Click).
  • David Mårtensson
    David Mårtensson over 13 years
    It should be available in the events list for the form if you use any version of visual studio.