简体   繁体   English

使用退出按钮后,FormClosing事件触发两次

[英]FormClosing event fires twice after using exit button

I have a button for exiting the form and this is the code 我有一个退出表单的按钮,这是代码

    DialogResult dialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit Program?", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.No)
    {
    }
    else
    {
        Application.Exit();
    }

I tried using debugger mode, and after I click yes, it goes through Application.Exit() and then fires up the FormClosing event then running the same dialog. 我尝试使用调试器模式,然后单击“是”,它将通过Application.Exit(),然后启动FormClosing事件,然后运行相同的对话框。

I also tried deleting the code in FormClosing event so it only has Application.Exit() but using Alt+F4 or clicking the X button will exit the application automatically. 我还尝试删除FormClosing事件中的代码,以便它仅具有Application.Exit(),但使用Alt + F4或单击X按钮将自动退出应用程序。

My question is how can I question the user if he wants to exit the program but not firing the dialog twice? 我的问题是我该如何询问用户是否要退出该程序但不两次触发对话框?

Thanks in advance, and I'm letting you all know I'm just a beginner and this is my biggest project so I want to make this great. 在此先感谢您,我让大家知道我只是一个初学者,这是我最大的项目,所以我想做得更好。

Here's an example. 这是一个例子。 It only asks for confirmation if the close was initiated by the user - you probably don't want a MessageBox popping up when Windows is restarting. 它仅询问确认关闭是否由用户启动-Windows重新启动时,您可能不希望弹出MessageBox。

private void form_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit Program?", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            Application.Exit();
        }
    }
    else 
    {
        // Cancel the close
        e.Cancel = true;
    }
 }

There are two ways to achieve this. 有两种方法可以实现此目的。

  • By unsubscribe the even on button click 取消订阅偶数点击按钮
  • As suggested in stuartd's answer, checking reason of closing (but there is a problem in his answer so adding this approach too with fix, so it will help future people.) 正如stuartd的答案中所建议的,检查关闭的原因(但是他的答案中有一个问题,因此在修复中也添加此方法,这样对将来的人会有所帮助。)

I am assuming, As you need this confirmation in both cases, button click and 'x' button click, you have put the same code in both handler. 我假设,在两种情况下都需要此确认,即单击按钮和单击“ x”按钮,因此您已经在两个处理程序中放入了相同的代码。

Approach one 方法一

In the handler of button click, while you are asking for user confirmation and if user is clicking 'yes'. 在您要求用户确认以及用户是否单击“是”时,在按钮单击的处理程序中。 Before the line, 上线之前

Application.Exit();

you should unsubscribe the Form closing event. 您应该退订表单关闭事件。 By doing this, It must not raise form closing event while performing Application.Exit() 这样,它在执行Application.Exit()时不得引发表单关闭事件。

assuming your form is MainForm and event is MainForm_Closing, it would look like, 假设您的表单是MainForm,事件是MainForm_Closing,则看起来

    private void btnClose_Click(object sender, EventArgs e)
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit Program?", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            this.FormClosing -= MainForm_FormClosing;
            Application.Exit();
        }
    }

so it will not raise form closing event while performing Application.Exit() and thus your problem will be solved. 因此在执行Application.Exit()时不会引发表单关闭事件,因此将解决您的问题。

Approach Two 方法二

As stuartd has suggested (which is more cleaner way according to me. +1 for that), you can check for form closing reason in Form Closing event handler. 正如stuartd所建议的那样(按照我的说法,这是一种更简洁的方法。为此+1),您可以在“ Form Closing事件处理程序中检查表单关闭的原因。

Note that there is a little problem (bug) in his sample code [which you have already accepted as an answer!!]. 请注意 ,他的示例代码中有一个小问题(错误)[您已经接受了它作为答案!!]。 After clicking 'x' button or Alt+F4 by mistake; 单击'x' button或错误地Alt+F4 if user clicks on 'No' on the confirmation message, then too form is being closed because there is no handling for else condition. 如果用户在确认消息上单击“否”,则也将关闭表单,因为没有其他条件的处理。 Proper solution should be like below. 正确的解决方案应如下所示。

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to exit?", "Exit Program?", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                    Application.Exit();
                else
                    e.Cancel = true;    //stopping Form Close perocess.
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM