Hey All, 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.
Hey All, 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.
What is happening when you click close? When you say close do you mean the Cross at the top right or is this a command button close?
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.
I don't understand what you mean by the question, when you press the "X" at the top right of the window the application will close on it's on accord.
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.
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