Windows Develop Bookmark and Share   
 index > Windows Forms General > FormClosingEvent has no CloseReason
 

FormClosingEvent has no CloseReason

In a Form I created, I've subscribed to the FormClosingEvent.
In this event I want to distinguish between a user that closes the form, or an outside OS event that closes the application. Therefor I read the CloseReason member of the FormClosingEventArgs. However it seems that when the user clicks on the OK button which is the Forms AcceptButton, the CloseReason seems to be set to "None". The same goes for the form's CancelButton.

Is this intended behaviour, and if not, should this not be documented?


Edsger  Friday, November 10, 2006 10:07 AM

When you handle the user clicking a close button that you have created on your form, you probably handle the application close with your own user code with a "this.Close();" command. When the form is actually closed with an Alt-F4 press or by clicking on the [X] - close button/box on the top-right corner, it is handled differently. Because the CloseReason field is ReadOnly, I am not sure how you would be able to make it so.

I would suggest that in your event handlers for the OK / Cancel button presses, you notify the other form of the impending "user-close" you will perform. Then in your other form where you see if the user closed the form, you can change the if statement to be:

if(e.CloseReason == CloseReason.UserClosing || userClose)
{
...
}

"userClose" will just be a boolean value that you change to true if the OK / Cancel button was pressed, and before you close that form.

John_Lee - MSFT  Wednesday, November 15, 2006 1:09 AM

I looked up the CloseReason enumeration on this page: MSDN CloseReasonand determined that the possible close reasons are:

Member name Description
ApplicationExitCall The Exit method of the Application class was invoked.
FormOwnerClosing The owner form is closing.
MdiFormClosing The parent form of this multiple document interface (MDI) form is closing.
None The cause of the closure was not defined or could not be determined.
TaskManagerClosing The Microsoft Windows Task Manager is closing the application.
UserClosing The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.
WindowsShutDown The operating system is closing all applications before shutting down.

I think in your case, you have a child form with an accept and cancel button, which caused the close. So it is not an ApplicationExitCall, as the application itself did not close. It's not a FormOwnerClosing or MdiFormClosing, since its owning form did not close. The Task Manager did not close the application. The user did not close using Alt-F4 or the Close Button. AndWindows did notclose the app, so it would fall under "none".

John_Lee - MSFT  Friday, November 10, 2006 6:13 PM

The description of the UserClosing event does not suggest that it only works with the window's close button or Alt+F4. It explicitly says that it is justan example.

The user is closing the form through the user interface (UI),

Does that not also includes my own controls? And if not, is there any way to make it so?

Edsger  Monday, November 13, 2006 5:47 PM

When you handle the user clicking a close button that you have created on your form, you probably handle the application close with your own user code with a "this.Close();" command. When the form is actually closed with an Alt-F4 press or by clicking on the [X] - close button/box on the top-right corner, it is handled differently. Because the CloseReason field is ReadOnly, I am not sure how you would be able to make it so.

I would suggest that in your event handlers for the OK / Cancel button presses, you notify the other form of the impending "user-close" you will perform. Then in your other form where you see if the user closed the form, you can change the if statement to be:

if(e.CloseReason == CloseReason.UserClosing || userClose)
{
...
}

"userClose" will just be a boolean value that you change to true if the OK / Cancel button was pressed, and before you close that form.

John_Lee - MSFT  Wednesday, November 15, 2006 1:09 AM

it was something I thought more of as a workaround (now there are two "closing" state variables), but if it's the only way it'll have to do.

Thanks!

Edsger

Edsger  Wednesday, November 15, 2006 9:27 AM

You can use google to search for other answers

Custom Search

More Threads

• Problem to get the information about user name while using windows service
• How to avoid SerializationException Unhandled error?
• Display context Menu Images in grid
• adding background image raises MissingManifestResourceException
• RichTextBox Scrollbars
• Can I add panel to statusStrip ?
• Showing List View Selection?
• Bug in Listbox Multiextended
• turn off GroupBox border
• Tray icon