When C # develops a WinForm form program, how do you close a window in a subform and exit the program?

Source: Internet
Author: User

In the development of the C#winform form program, has not encountered, in the process of running or debugging procedures, clearly closed the child window, the program did not quit,

We can not edit, to manually turn off the Stop debugging or all the interruption of the operation, the program just quit, this will not feel very annoying.

I would simply like to share my experience:

1, first we open the properties of the child window, click on the "Lightning icon (below)" Found formclosing (or formclosed) event, double-click


2. Edit code in gameui_formclosing event


If you want to give a prompt box when the user closes the child window program, you can add the following code:


private void Gameui_formclosing (object sender, FormClosingEventArgs e)
        {
            If MessageBox.Show ("Closes the form, the program quits ... ", "。。 Hint ", messageboxbuttons.yesno) = = Dialogresult.yes)
            {
                e.cancel = false;
                System.Environment.Exit (0);
            }
            else
            {
                E.cancel = true;
            }
        }
Note: The above code is written in the FormClosing () event


Exiting the WinForm program in C # includes a number of methods, such as:

This. Close (); Application.exit (); Application.exitthread (); System.Environment.Exit (0); Wait

1.this.   Close (); Just close the current window, if not the main form, can not quit the program, and if there is a managed thread (not main thread), also can not cleanly exit;

2.application.exit (); Forces all messages to abort, exits all forms, but does not cleanly exit if there is a managed thread (not the primary thread);

3.application.exitthread (); Forcing all messages on the calling thread to abort, as well as the problem of other threads not exiting correctly;

4.system.environment.exit (0); This is the most thorough exit way, no matter what thread is forced to quit, the program ended very clean.


Hope to help you ...

Welcome to discuss in the comments section, what is wrong, welcome to point out.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.