tags:

views:

283

answers:

6

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.

A: 

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?

Jim
Yes, I mean the cross at the top of the window.
Justin
You shouldn't need to add anything, so I suspect something is intercepting your event - unless you've removed some auto code.Can you step through the put a breakpoint after the form load, start to step through so that the form is displayed. Then click the close button. Step through and see where the code goes.Also, try addign a new form and closing that - does it work?
Jim
A: 

Maybe you are looking for the Application.Exit method.

Konamiman
+1  A: 

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.

Fredrik Mörk
A: 

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.

Jamie K
*Should* not *will* - otherwise the question wouldn't have arisen...
Murph
+1  A: 

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.

Jose Basilio
+2  A: 

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

Henk Holterman
I have tried your solution but it is not working. I missed one point. In the second form I am starting a thread. Could this be any interfering point?
Justin
Justin, Yes, if the thread is __not__ a background thread it will keep your process running.
Henk Holterman
Yes my thread had an open TcpClient object. All I needed was to close this object and it worked very fine!! Thanks Henk for the guidance!
Justin