c# Windows 窗体:窗体关闭事件取消按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6671504/
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
c# Windows Forms: Form Close Event Cancel Button
提问by jonalodev
I have a Form_Closing event that prompts the user if the file has been changed to save if changes have been made to the file (standard Yes/No/Cancel options). Cancel is where things don't work as they should.
我有一个 Form_Closing 事件,如果对文件进行了更改(标准是/否/取消选项),它会提示用户文件是否已更改以保存。取消是事情不能正常工作的地方。
If I select File -> New and there is an existing file with changes I get prompted as expected, bit when I select Cancel the new form is presented rather than staying on the current form and I end up with two forms open at once.
如果我选择 File -> New 并且有一个包含更改的现有文件,我会按预期提示我,当我选择 Cancel 时,会显示新表单而不是停留在当前表单上,我最终会同时打开两个表单。
Here is MainForm (File New) code:
这是 MainForm (File New) 代码:
if (editForm != null)
{
// Close existing Editor form
editForm.Close();
// Open new form
editForm = new EditorForm(this);
// Close Form Events
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.Show();
editForm.Focus();
else
{
// Open new Editor
editForm = new EditorForm(this);
// Close Form Events
editForm.Closing += new CancelEventHandler(EditorForm_Closing);
editForm.Show();
editForm.Focus();
}
Here is my EditForm_Closing:
这是我的 EditForm_Closing:
if (editForm != null)
{
if (editForm.diagramComponent.Model.Modified)
{
DialogResult res = MessageBox.Show(this, "The project has been modified. Save changes?", "Save changes", MessageBoxButtons.YesNoCancel);
if (res == DialogResult.Yes)
{
if (!editForm.HasFileName)
{
if (this.saveEditorDialog1.ShowDialog(this) == DialogResult.OK)
{
this.ActiveDiagram.SaveSoap(this.saveEditorDialog1.FileName);
editForm.FileName = this.saveEditorDialog1.FileName;
}
}
else
{
this.ActiveDiagram.SaveSoap(editForm.FileName);
}
}
else if (res == DialogResult.Cancel)
{
e.Cancel = true;
}
}
Not sure how to make the correlation between the Cancel close event and my File -> New. Any help is greatly appreciated. Thank you.
不确定如何在取消关闭事件和我的文件 -> 新建之间建立关联。任何帮助是极大的赞赏。谢谢你。
EDIT:Added my EditForm_Closing Event.
编辑:添加了我的 EditForm_Closing 事件。
采纳答案by Sergei Z
Try replacing your main form's code with the following:
尝试使用以下内容替换主表单的代码:
if (editForm != null) { // try closing existing Editor form editForm.Close(); if(!editForm.IsDisposed) // close was canceled. return; } // Open new form editForm = new EditorForm(this); // Close Form Events editForm.FormClosing += new FormClosingEventHandler('suitable method here'); editForm.Show(); editForm.Focus();
回答by Hans Passant
Your Closing event handler should set the editForm property back to null. So check it like this:
您的 Closing 事件处理程序应该将 editForm 属性设置回 null。所以像这样检查它:
if (editForm != null) {
editForm.Close();
if (editForm != null) return; // Close was cancelled
// etc..
}
Or just use a private boolean member.
或者只使用私有布尔成员。