C# Windows Forms:窗体关闭事件取消按钮

3
我有一个Form_Closing事件,如果文件已被更改,则提示用户保存已对文件进行的更改(标准的是/否/取消选项)。问题出现在取消操作。
如果我选择“文件”-> “新建”,并且存在已更改的文件,则按预期会出现提示,但当我选择“取消”时,新表单将被呈现,而不是停留在当前表单上,并且最终我将同时打开两个表单。
以下是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();
        }

这是我的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;
                }
            }

我不确定如何将取消关闭事件与“文件”->“新建”之间的关联起来。非常感谢您的任何帮助。谢谢。

编辑:添加了我的EditForm_Closing事件。


你能在EditorForm_Closing中也展示代码吗? - shelleybutterfly
还有处理显示对话框的代码吗? - shelleybutterfly
看起来取消操作已经被正确处理了,我唯一能想到的是在你的“文件->新建”代码中,在所示的调用之前,某种方式将editForm设置为null。如果没有什么问题引人注目,我会尝试在你粘贴的“文件->新建”代码顶部设置断点,以查看发生了什么。 - shelleybutterfly
移除了我的回答,因为它似乎不相关。所以我不能确定并且没有时间测试,但也许你需要在Form_Closing事件中调用窗口的Close()方法? - shelleybutterfly
2个回答

2

您的关闭事件处理程序应该将editForm属性设置为null。因此,请像这样检查它:

if (editForm != null) {
    editForm.Close();
    if (editForm != null) return;  // Close was cancelled
    // etc..
}

或者只需使用一个私有布尔成员。


啊啊啊,我在我的DialogResult.Cancel加了一个return;,当我试图关闭窗体时,似乎起作用了,谢谢!不过,当我点击文件 -> 新建时,它仍然会打开一个第二个窗口。 - jonalodev
在Closing事件处理程序中,您是否将editForm字段设置为null?重要的是,您不希望泄漏实例。 - Hans Passant

2
尝试使用以下代码替换您的主表单代码:
if (editForm != null) {
    // 尝试关闭现有的编辑器窗体
    editForm.Close();
    if(!editForm.IsDisposed) // 关闭被取消了。
        return;
}
// 打开新窗体 editForm = new EditorForm(this); // 关闭窗体事件 editForm.FormClosing += new FormClosingEventHandler('适当的方法名称'); editForm.Show(); editForm.Focus();

我无法测试它是否有效,如果有效,请确保在处理程序中适当地断开FormClosingEventHandler(如果您不取消,则允许编辑器关闭)。 - Sergei Z

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接