title: .NET Windows Forms - intercepting the Close X event
body: <p>This must be a dumb question, but I cannot figure it out. I also cannot use the designer because coders before me managed to throw GUI and logic all in one, so now it is confused. I've got to do it the old school way.</p>
  
  <p>I have a Form which can be closed in 3 ways: Close button, File / Close menu, and the X icon. I want them all to do the same thing. Intercepting the button and the menu events is easy. In fact, both are hooked up to an <code>onCloseConfig</code> method. Btw, is there a better name for this method?</p>
  
  <pre><code>private void onCloseConfig(object sender, System.EventArgs e)
  {
      if (! m_configControl.Modified)
      {
          Application.Exit(); // Or should it be this.Close();
      }
      ....
      // Else present a dialog, ask if they want to save.
  }
  </code></pre>
  
  <p>So, to intercept the <code>X</code> I tried: <code>this.FormClosing +=new FormClosingEventHandler(this.onCloseConfig);</code> I believe this is what causes an infinite loop. I do not want that :) <code>FormClosed</code> is another option, but it seems too late. I just want to intercept the fact that the X was clicked, not the fact that the form is closing.</p>