Justin Samuel
Justin Samuel

Reputation: 1083

C# - window form application - Close the application

I want a help. I have a window form application. Whenever I click on the "close" of the form, the application should itself close.

Can anyone help me.

Regards, Justin Samuel.

Upvotes: 4

Views: 17142

Answers (4)

Konamiman
Konamiman

Reputation: 50323

Maybe you are looking for the Application.Exit() method.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273804

After your explanation:

In Form1, do something like:

  Form2 f2 = new Form2();
  f2.ShowDialog();
  this.Close();
  Application.Exit();

This is assuming Form2 can be shown Modal (Dialog), which I think is correct

Upvotes: 6

Jose Basilio
Jose Basilio

Reputation: 51548

By your description, it sounds like you may have multiple forms in your application and one form is still running even if its's not visible. In this case, the close button, would close the form, but not exit the application.

You need to add an event handler for the Form_FormClosed event and then call Application.Exit() - Ideally, you could also handle the Form_FormClosing event if you want to give the user a dialog to ask if they really meant to close.

Upvotes: 2

Fredrik Mörk
Fredrik Mörk

Reputation: 158389

A Winforms application will exit automatically when the main form is closed. The main form is the form that is instantiated and passed to Application.Run method in the Main method of the application.

If the application process does not exit when this form is closed, something is preventing it from closing, such as a thread (that is not a background thread) that is performing some work.

Upvotes: 2

Related Questions