


CloseReason enumeration of Args parameters in FormClosing and FormClosed events in C# language
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; } }
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, } }
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();
(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();
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.
