开发者

C# - window form application - Close the application

I want a help. I have a windo开发者_运维知识库w form application. Whenever I click on the "close" of the form, the application should itself close.

Can anyone help me.

Regards, Justin Samuel.


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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜