C# 如何防止或阻止关闭 WinForms 窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12409529/
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
How to prevent or block closing a WinForms window?
提问by
How can I prevent window closing by showing a MessageBox? (Technology:WinFormswith C#)
如何通过显示 来防止窗口关闭MessageBox?(技术:WinForms与C#)
When the close event occurs I want the following code to be run:
当关闭事件发生时,我希望运行以下代码:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
//close addFile form
} else {
//ignore closing event
}
}
采纳答案by Victor
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
var window = MessageBox.Show(
"Close the window?",
"Are you sure?",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.No);
}
回答by Erre Efe
Within your OnFormClosingevent you can show the dialog and ifanswer is false (to not show) then set the Cancelproperty of the EventArgsto true.
在您的OnFormClosing活动中,您可以显示对话框并且if答案为 false(不显示),然后将 的Cancel属性设置EventArgs为true。
回答by BlueM
Catch FormClosing event and set e.Cancel = true
捕获 FormClosing 事件并设置 e.Cancel = true
private void AdminFrame_FormClosing(object sender, FormClosingEventArgs e)
{
var res = MessageBox.Show(this, "You really want to quit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (res != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
回答by D Stanley
Straight from MSDN:
直接来自MSDN:
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...
}
}
}
In your case you don't need to do anything to explicitly close the form. Unless you cancel it it will close automatically, so your code would be:
在您的情况下,您无需执行任何操作即可明确关闭表单。除非您取消它,否则它将自动关闭,因此您的代码将是:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
// do nothing
} else {
e.Cancel = true;
}
}
回答by daniele3004
For prevent or block the form closing in particular situation you can use this strategy:
为了在特定情况下防止或阻止表单关闭,您可以使用以下策略:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FLAG_CONDITION == true)
{
MessageBox.Show("To exit save the change!!");
e.Cancel = true;
}
}
回答by Lars I Nielsen
A special twist might be to always prevent just the user closing the form:
一个特殊的转折可能是始终防止用户关闭表单:
private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
// disable user closing the form, but no one else
}
回答by XxPKBxX
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
e.Cancel = false;
} else {
e.Cancel = true;
}
e.Cancel is to enable or disable form closing event.
For example, e.Cancel = truewill disable you closing.
e.Cancel 是启用或禁用表单关闭事件。例如,e.Cancel = true将禁止您关闭。

