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.
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.
Here is MainForm (File New) code:
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:
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.
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();