Checking to see if a C# WinForm is closed

Jpin picture Jpin · Feb 22, 2012 · Viewed 7.1k times · Source

I am using the FormClosing event on a C# Windows Form to ask the user to confirm before exiting. I only wish to show this message if the user specifically closes the form using the red cross in the corner of the form window. When I close or hide a form manually I do not wish to show the confirm message (e.g. manually calling form.Close()).

Is it possible to check to see if the user has pressed the red cross?

Thanks.

Answer

Sjoerd picture Sjoerd · Feb 22, 2012

Check the FormClosingEventArgs.CloseReason property.