CloseReason enumeration of Args parameters in FormClosing and FormClosed events in C# language

高洛峰
Release: 2016-11-22 16:17:15
Original
2185 people have browsed it

My operating system is Win7 Ultimate, the VS version is VS2012, and the .NET version is .NET Framework 4.5.

In the FormClosing event of the form, there is an enumeration variable CloseReason under the second parameter (FormClosingEventArgs type), and in the FormClosed event of the form, there is also an enumeration variable CloseReason under the second parameter (FormClosedEventArgs type). The CloseReason enumeration is under the namespace System.Windows.Forms.

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

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"); } break;
        case CloseReason.MdiFormClosing: { MessageBox.Show("Closing: CloseReason.MdiFormClosing"); } break;
        case CloseReason.UserClosing: { MessageBox.Show("Closing: CloseReason.UserClosing"); } break;
        case CloseReason.TaskManagerClosing: { MessageBox.Show("Closing: CloseReason.TaskManagerClosing"); } break;
        case CloseReason.FormOwnerClosing: { MessageBox.Show("Closing: CloseReason.FormOwnerClosing"); } break;
        case CloseReason.ApplicationExitCall: { MessageBox.Show("Closing: CloseReason.ApplicationExitCall"); } break;
    }
}
Copy after login

From the metadata, this enumeration has the following 7 enumeration values:

#region 程序集 System.Windows.Forms.dll, v4.0.0.0
// C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Windows.Forms.dll
#endregion

using System;

namespace System.Windows.Forms
{
    // 摘要:
    //     指定窗体关闭的原因。
    public enum CloseReason
    {
        // 摘要:
        //     关闭原因未定义或者无法确定。
        None = 0,
        //
        // 摘要:
        //     操作系统正在关闭所有应用程序以便准备关机。
        WindowsShutDown = 1,
        //
        // 摘要:
        //     此多文档界面 (MDI) 窗体的父窗体正在关闭。
        MdiFormClosing = 2,
        //
        // 摘要:
        //     用户正在通过用户界面 (UI) 关闭该窗体,例如通过单击窗体窗口上的“关闭”按钮,通过选择窗口控制菜单上的“关闭”按钮,或者通过按 Alt+F4
        //     等方式关闭。
        UserClosing = 3,
        //
        // 摘要:
        //     Microsoft Windows 任务管理器正在关闭应用程序。
        TaskManagerClosing = 4,
        //
        // 摘要:
        //     所有者窗体正在关闭。
        FormOwnerClosing = 5,
        //
        // 摘要:
        //     System.Windows.Forms.Application 类的 System.Windows.Forms.Application.Exit()
        //     方法被调用。
        ApplicationExitCall = 6,
    }
}
Copy after login

The MSDN of this enumeration can refer to the page:

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

Excluding the None type, this article tested all 6 enumeration values, and the test results are recorded here.

1. CloseReason.WindowsShutDown

This CloseReason is triggered when Windows is logged out or closed. However, do not add elements such as MessageBox here, because once Windows finds that the current program cannot be closed, it will forcefully close the program.

2. CloseReason.MdiFormClosing

When the current form is an Mdi subform and the Mdi container form is closed, this CloseReason is prompted when the current FormClosing and FormClosed events are triggered.

How to use the current form as an MdiParent to open another form:

FormChild formChild = new FormChild();
formChild.MdiParent = this;
formChild.Show();
Copy after login

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

3. CloseReason.UserClosing

The user manually closes the current program, such as calling Close( ) function, or click the "×" in the upper right corner of the program, the closing reason is CloseReason.UserClosing.

4. CloseReason.TaskManagerClosing

This event will be triggered when the window is closed by the task manager, but during testing, I found that it is mandatory for the task manager to close the window. After setting the breakpoint, you can find that the program will be forcibly closed by the task manager shortly after the FormClosing event is triggered. This time is very short, so it is not suitable to do things such as popping up the MessageBox here (because it is useless).

5. CloseReason.FormOwnerClosing

Similar to CloseReason.MdiFormClosing, if form A is the owner of form B, then when form A is closed, this CloseReason is used when form B triggers the FormClosing and FormClosed events.

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

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

Method to use the current form as the Owner to open another form:

FormChild formChild2 = new FormChild();
formChild2.Owner = this;
formChild2.Show();
Copy after login

6. CloseReason.ApplicationExitCall

When calling the Application.Exit() method to exit the program, CloseReason is this value.

Finally, let’s talk about the calling sequence of FormClosing and FormClosed events:

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

2. If form A is an mdi container, form B’s mdi-parent is form A, then the calling sequence of events is:

Form B - FormClosing event - CloseReason. MdiFormClosing

Form A - FormClosed event - CloseReason.UserClosing

3. If form A is the owner of form B, the calling order of the events is:

Form B - FormClosing event - CloseReason.FormOwnerClosing

Window Form A - FormClosing event - CloseReason.UserClosing

Form B - FormClosed event - CloseReason.FormOwnerClosing

Form A - FormClosed event - CloseReason.UserClosing

Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!