title: c# Windows Forms: Form Close Event Cancel Button
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is MainForm (File New) code:</p>
  
  <pre><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();
          }
  </code></pre>
  
  <p>Here is my EditForm_Closing:</p>
  
  <pre><code>  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;
                  }
              }
  </code></pre>
  
  <p>Not sure how to make the correlation between the Cancel close event and my File -> New. Any help is greatly appreciated. Thank you.</p>
  
  <p><strong>EDIT:</strong> Added my EditForm_Closing Event.</p>