title: Canceling closing of C# Form
body: <p>My program has two ways to close, one being the 'X' in the top-right corner and the other being an 'Exit' button. Now, when either one of these is pressed when a certain condition is met, a message pops up notifying the user that they haven't saved yet. If they DID save, the message won't pop up and the program closes as normal. Now, when the message DOES pop up, the user gets a MessageBox with Yes and No buttons. If 'Yes' is pressed, the program needs to save. If 'No' is pressed, the program needs to cancel the close event that has been initiated when the user pressed the 'X' or 'Exit' button.</p>
  
  <p>What is the best way to do this?</p>
  
  <pre><code>    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
      TryClose();
  }
  
  private void TryClose()
  {
      if (saved == false)
      {
          //You forgot to save
          //Turn back to program and cancel closing event
      }
  }
  </code></pre>