none
Capture Form Close Click Event (Red X)

    Question

  • Hi Everyone,

    Could you please help with a little problem. I have code a very simple Windows Form that closes the form. I want to capture an event where the user has instigated the close by clicking the Close Button (red x), rather than a close instigated by code.

    The Closing event fires when both ways of closing the form is used, but I want to differentiate between the two ways and capture the click event of the Close Button.

    Any ideas where I could find information on this?

    Thanks,
    Shai
    Wednesday, July 16, 2008 12:26 AM

Answers

  • Hi,

    In the FormClosing event th FormClosingEventArg has a property tha's called CloseReason. This is very usefull becouse if you want the Form to stop closing if the closing reason was CloseReason.UserClosing you can do (FromClosingEventArg.Cancel = true ) then do something in your code and finally close the form programatically(or use another logic ). The FormClosedEventArg has the same CloseReason property but then you can't stop the Form from closing.

    Hope this help.

    Camilo
    • Edited by olimac Wednesday, July 16, 2008 1:04 AM spell
    • Marked as answer by shai_au Wednesday, July 16, 2008 1:06 AM
    Wednesday, July 16, 2008 12:57 AM

All replies

  • Hi,

    In the FormClosing event th FormClosingEventArg has a property tha's called CloseReason. This is very usefull becouse if you want the Form to stop closing if the closing reason was CloseReason.UserClosing you can do (FromClosingEventArg.Cancel = true ) then do something in your code and finally close the form programatically(or use another logic ). The FormClosedEventArg has the same CloseReason property but then you can't stop the Form from closing.

    Hope this help.

    Camilo
    • Edited by olimac Wednesday, July 16, 2008 1:04 AM spell
    • Marked as answer by shai_au Wednesday, July 16, 2008 1:06 AM
    Wednesday, July 16, 2008 12:57 AM
  • Exactly what I am looking for, thanks Camilo :)
    Wednesday, July 16, 2008 1:06 AM
  • The problem with this is that both programmatic close and the Red X has the same CloeseReason. : Userclosing

    So if I want to delay the close triggered by the user, do some cleanup and then do a form1.Close() I'll be stuck in an endless loop

    Friday, January 27, 2012 11:45 PM