C# 检测表单关闭的原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1623756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:28:54  来源:igfitidea点击:

Detect reason for form closing

c#winforms

提问by user

How can I detect how a windows form is being closed? For example, how do I find out whether the user has clicked on a button which closes the form or if the user clicks on the "X" in the upper-right? Thank you.

如何检测窗体是如何关闭的?例如,我如何确定用户是否点击了关闭表单的按钮,或者用户是否点击了右上角的“X”?谢谢你。

Update:

更新:

Forgot to mention that the button calls the Application.Exit() method.

忘记提到按钮调用 Application.Exit() 方法。

采纳答案by Oliver

As bashmohandes and Dmitriy Matveev already mentioned the solution should be the FormClosingEventArgs. But as Dmitriy also said, this wouldn't make any difference between your button and the X in the right upper corner.

正如 bashmohandes 和 Dmitriy Matveev 已经提到的,解决方案应该是 FormClosingEventArgs。但正如德米特里所说,这不会对您的按钮和右上角的 X 产生任何影响。

To distinguish between these two options, you can add a boolean property ExitButtonClickedto your form and set it to true in the button Click-Event right before you call Application.Exit().

为了区分这两个选项,您可以在表单中添加一个布尔属性ExitButtonClicked,并在调用 Application.Exit() 之前在按钮 Click-Event 中将其设置为 true。

Now you can ask this property within the FormClosing event and distinguish between those two options within the case UserClosing.

现在,您可以在活动的FormClosing内问这个属性和机箱内的两个选项之间区别UserClosing

Example:

例子:

    public bool UserClosing { get; set; }

    public FormMain()
    {
        InitializeComponent();

        UserClosing = false;
        this.buttonExit.Click += new EventHandler(buttonExit_Click);
        this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    }

    void buttonExit_Click(object sender, EventArgs e)
    {
        UserClosing = true;
        this.Close();
    }

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        switch (e.CloseReason)
        {
            case CloseReason.ApplicationExitCall:
                break;
            case CloseReason.FormOwnerClosing:
                break;
            case CloseReason.MdiFormClosing:
                break;
            case CloseReason.None:
                break;
            case CloseReason.TaskManagerClosing:
                break;
            case CloseReason.UserClosing:
                if (UserClosing)
                {
                    //what should happen if the user hitted the button?
                }
                else
                {
                    //what should happen if the user hitted the x in the upper right corner?
                }
                break;
            case CloseReason.WindowsShutDown:
                break;
            default:
                break;
        }

        // Set it back to false, just for the case e.Cancel was set to true
        // and the closing was aborted.
        UserClosing = false;
    }

回答by okutane

You can check CloseReason property of FormClosingEventArgs in FormClosing event handler to check some of the possible cases. However, cases described by you will be indistinguishable if you will only use this property. You will need to write some additional code in click event handler of your "close" button to store some information which will be checked in the FormClosing event handler to distinguish between these cases.

您可以在 FormClosing 事件处理程序中检查 FormClosingEventArgs 的 CloseReason 属性以检查一些可能的情况。但是,如果您仅使用此属性,则您描述的情况将无法区分。您需要在“关闭”按钮的 click 事件处理程序中编写一些额外的代码来存储一些信息,这些信息将在 FormClosing 事件处理程序中检查以区分这些情况。

回答by bashmohandes

You need to add a listener to the Even FormClosing, which sends in the event args a property of type CloseReason which is one of these values

您需要向 Even FormClosing 添加一个侦听器,它会在事件 args 中发送一个 CloseReason 类型的属性,它是这些值之一

    // Summary:
//     Specifies the reason that a form was closed.
public enum CloseReason
{
    // Summary:
    //     The cause of the closure was not defined or could not be determined.
    None = 0,
    //
    // Summary:
    //     The operating system is closing all applications before shutting down.
    WindowsShutDown = 1,
    //
    // Summary:
    //     The parent form of this multiple document interface (MDI) form is closing.
    MdiFormClosing = 2,
    //
    // Summary:
    //     The user is closing the form through the user interface (UI), for example
    //     by clicking the Close button on the form window, selecting Close from the
    //     window's control menu, or pressing ALT+F4.
    UserClosing = 3,
    //
    // Summary:
    //     The Microsoft Windows Task Manager is closing the application.
    TaskManagerClosing = 4,
    //
    // Summary:
    //     The owner form is closing.
    FormOwnerClosing = 5,
    //
    // Summary:
    //     The System.Windows.Forms.Application.Exit() method of the System.Windows.Forms.Application
    //     class was invoked.
    ApplicationExitCall = 6,
}