c# Windows Forms: Form Close Event Cancel Button

11,021

Solution 1

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();

Solution 2

Your Closing event handler should set the editForm property back to null. So check it like this:

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

Or just use a private boolean member.

Share:
11,021
jonalodev
Author by

jonalodev

Updated on June 04, 2022

Comments

  • jonalodev
    jonalodev almost 2 years

    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.

  • jonalodev
    jonalodev almost 13 years
    ahhh, i added return; to my DialogResult.Cancel and that seemed to work when i try to just close the form thank you! however, when i click file -> new it still opens a second window.
  • user1703401
    user1703401 almost 13 years
    Are you setting the editForm field to null in the Closing event handler? Important, you don't want to leak the instance.
  • Sergei Z
    Sergei Z almost 13 years
    I cannot test whether it works, and if it does, make sure you disconnect the FormClosingEventHandler appropriately in the handler as well (if you're not canceling, that is, allowing the editor to close).