


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

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use