Home Backend Development C#.Net Tutorial CloseReason enumeration of Args parameters in FormClosing and FormClosed events in C# language

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

Nov 22, 2016 pm 04:17 PM
c#

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

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

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

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

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

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

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

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.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

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.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles