C# 如何捕获退出 Winforms 应用程序的事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11047475/
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-09 16:15:59  来源:igfitidea点击:

How do I catch the event of exiting a Winforms application?

c#.netwinforms

提问by l46kok

If the user wants to exit the application by clicking on the exit icon or by ALT+F4, I'd like to make a dialog box questioning the user if he/she is really sure about exiting.

如果用户想通过单击退出图标或按 ALT+F4 退出应用程序,我想创建一个对话框,询问用户是否真的确定要退出。

How do I capture this event before the application is actually closed?

如何在应用程序实际关闭之前捕获此事件?

采纳答案by musefan

Check out the OnClosingevent for the form.

查看表单的OnClosing事件。

Here is an extract from that link actually checks for a change on a text field and prompts a save:

以下是该链接的摘录,实际检查文本字段的更改并提示保存:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // Determine if text has changed in the textbox by comparing to original text.
   if (textBox1.Text != strMyOriginalText)
   {
      // Display a MsgBox asking the user to save changes or abort.
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   }
}

You can change the text to suit your needs, and then also I think you might want to switch the DialogResult.Yesto DialogResult.Nobased on your text.

您可以更改文本以满足您的需要,然后我认为您可能希望根据您的文本切换DialogResult.YesDialogResult.No



Here is some modified code just for you:

这是一些专门为您修改的代码:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   if(MessageBox.Show("Are you sure you want to quit?", "My Application", MessageBoxButtons.YesNo) ==  DialogResult.No)
   {
      e.Cancel = true;
   }
}

回答by Jon Skeet

Is this just one form? If so, you might want to use the FormClosingevent, which allows you to cancel it (show the dialog, then set CancelEventArgs.Cancelto true if the user chooses to cancel closing).

这只是一种形式吗?如果是这样,您可能想要使用该FormClosing事件,它允许您取消它(显示对话框,CancelEventArgs.Cancel如果用户选择取消关闭,则设置为 true)。

回答by Bali C

You can handle the Form_Closingor Form_Closedevents for this.

您可以为此处理Form_ClosingForm_Closed事件。

In Visual Studio click the lightning bolt icon and scroll down to these events in the form properties list. Double click on the one you want and it will hook up the event for you.

在 Visual Studio 中,单击闪电图标并向下滚动到表单属性列表中的这些事件。双击你想要的那个,它会为你连接事件。

回答by Steve

You should subscribe to the Form_Closing event
Post a dialog box there, and if user abort the closing set the FormCloseEventArgs.Cancel to true.

您应该订阅 Form_Closing 事件
在那里发布一个对话框,如果用户中止关闭,则将 FormCloseEventArgs.Cancel 设置为 true。

For example in the Form_Load or using the designer, subscribe the event

比如在Form_Load中或者使用设计器,订阅事件

Form1.FormClosing += new FormClosingEventHandler(Form1_Closing);

....
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) 
{
    DialogResult d = MessageBox.Show("Confirm closing", "AppTitle", MessageBoxButtons.YesNo );
    if(d == DialogResult.No)
        e.Cancel = true;
}

Depending on the situation is not always a good thing to annoy the user with this kind of processing.
If you have valuable modifieddata and don't want the risk to loose the changes, then it is always a good thing to do, but if you use this only as a confirm of the close action then it's better to do nothing.

视情况而定,用这种处理方式来惹恼用户并不总是一件好事。
如果您有有价值的修改数据并且不希望风险丢失更改,那么这样做总是一件好事,但是如果您仅将此用作关闭操作的确认,那么最好什么都不做。

回答by Tigran

If you're talking about windows forms, should be enough to catch your MainWindow's

如果你在谈论windows forms,应该足以抓住你的MainWindow 的

FormClosingevent and in case you want preventclosing, just set the argument of the event handler got fired, to true.

FormClosing事件,如果您想阻止关闭,只需将事件处理程序的参数设置为true.

Example:

例子:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{

      if(MessageBox.Show("Do you really want to exit?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.No){

                // SET TO TRUE, SO CLOSING OF THE MAIN FORM, 
                // SO THE APP ITSELF IS BLOCKED
                e.Cancel = true;            
      }
   }
}