The Closereason enumeration of the args parameter in C # language formclosing and formclosed events

Source: Internet
Author: User
My operating system is Win7 flagship version, vs version is VS2012,. NET version is the. NET Framework 4.5.

In the form of the FormClosing event, the second parameter (FormClosingEventArgs type) has an enumeration variable Closereason, formclosed event in the form, The second parameter (Formclosedeventargs type) also has an enumeration variable Closereason. This Closereason enumeration is under namespace System.Windows.Forms.

As shown in the following code, Closereason is in the FormClosingEventArgs type variable e of the form FormClosing event.

private void Formmain_formclosing (object sender, FormClosingEventArgs e) {    switch (E.closereason)    {        case Closereason.none: {MessageBox.Show ("Closing:CloseReason.None");} break;        Case Closereason.windowsshutdown: {MessageBox.Show ("Closing:CloseReason.WindowsShutDown");        Case closereason.mdiformclosing: {MessageBox.Show ("Closing:CloseReason.MdiFormClosing");        Case closereason.userclosing: {MessageBox.Show ("Closing:CloseReason.UserClosing");        Case closereason.taskmanagerclosing: {MessageBox.Show ("Closing:CloseReason.TaskManagerClosing");        Case closereason.formownerclosing: {MessageBox.Show ("Closing:CloseReason.FormOwnerClosing");        Case Closereason.applicationexitcall: {MessageBox.Show (' Closing:CloseReason.ApplicationExitCall ');} break;}    }

In terms of metadata, the enumeration has the following 7 enumerated values:

#region Assembly System.Windows.Forms.dll, v4.0.0.0//C:\Program files\reference Assemblies\microsoft\framework\. Netframework\v4.0\system.windows.forms.dll#endregionusing system;namespace system.windows.forms{//Abstract://Specify form off    The cause of the closure.        public enum Closereason {//Summary://shutdown reason undefined or undefined.        None = 0,/////Abstract://The operating system is shutting down all applications to prepare for shutdown.        Windowsshutdown = 1,/////Summary://The parent form of this multiple document interface (MDI) Form is closing. mdiformclosing = 2,/////Abstract://The user is closing the form through the user interface (UI), for example by clicking the Close button on the Form window, by selecting the Close button on the window's control menu, or by        Press ALT+F4//etc. to close.        userclosing = 3,/////Summary://Microsoft Windows Task Manager is shutting down the application.        taskmanagerclosing = 4,/////Summary://The owner form is closing. formownerclosing = 5,////Abstract://System.Windows.Forms.Application class of System.Windows.Forms.Appli cation.        Exit ()//method is called. ApplicationexitcAll = 6,}} 

MSDN for this enumeration can refer to the page:

Https://msdn.microsoft.com/en-us/library/system.windows.forms.closereason (v=vs.110). aspx

Removing the None type, this article tests 6 enumeration values, where the results of the test are recorded.

1, Closereason.windowsshutdown

This closereason is triggered when Windows is logged off and off, but do not add elements like the MessageBox here, because once Windows discovers that the current program is too late to shut down, it forces the program to close.

2, Closereason.mdiformclosing

When the current form is an MDI child form, when the MDI container form is closed, this closereason is prompted when the current formclosing and formclosed events are triggered.

To open the current form as a MdiParent method for another form:

Formchild formchild = new Formchild (); formchild.mdiparent = This;formchild.show ();

(You need to set the IsMdiContainer of this form to true)

3, Closereason.userclosing

The user closes the current program manually, such as by calling the close () function, or by tapping the "X" in the upper-right corner of the program, and the cause is closereason.userclosing.

4, Closereason.taskmanagerclosing

This event is triggered when the window is closed by Task Manager, but I found in the test that the Task Manager close window is mandatory. After setting a breakpoint, it can be found that shortly after the FormClosing event is triggered, the program is forced to close by the task Manager, which is a very short time, so it is not appropriate to do something like pop-up the MessageBox (because it is useless).

5, Closereason.formownerclosing

Similar to closereason.mdiformclosing, if form A is owner of form B, this closereason is used when form a closes, form B triggers formclosing and formclosed events.

For questions about forms as owner, you can refer to the MSDN page:

Https://msdn.microsoft.com/en-us/library/system.windows.window.owner (v=vs.110). aspx

To open another form as owner of the current form:

Formchild formChild2 = new Formchild (); formchild2.owner = This;formchild2.show ();

6, Closereason.applicationexitcall

This value is Closereason when the Application.exit () method is called to exit the program.

Finally, the order of the formclosing and formclosed events is called:

1. The FormClosing event is triggered before the form closes, and the FormClosed event is triggered after the form is closed

2. If form A is an MDI container and Form B's mdi-parent is form a, then the order in which the events are called is:

Form B-formclosing Event-closereason.mdiformclosing

Form A-formclosing Event-closereason.userclosing

Form B-formclosed Event-closereason.mdiformclosing

Form A-formclosed Event-closereason.userclosing

3. If form A is owner of form B, then the order in which the events are called is:

Form B-formclosing Event-closereason.formownerclosing

Form A-formclosing Event-closereason.userclosing

Form B-formclosed Event-closereason.formownerclosing

Form A-formclosed Event-closereason.userclosing

  • Related Article

    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.