.Net forms - intercepting the Close X event.

Posted by Hamish Grubijan on Stack Overflow See other posts from Stack Overflow or by Hamish Grubijan
Published on 2010-05-10T15:24:06Z Indexed on 2010/05/10 15:44 UTC
Read the original article Hit count: 122

Filed under:
|

Hi, 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.

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 onCloseConfig method. Btw, is there a better name for this method?

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.
}

So, to intercept the X I tried: this.FormClosing +=new FormClosingEventHandler(this.onCloseConfig); I believe this is what causes an infinite loop. I do not want that :) FormClosed 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. Please help.

Thanks!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about winforms