How can I detect how a windows form is being closed? For example, how do I find out whether the user has clicked on a button which closes the form or if the user clicks on the "X" in the upper-right? Thank you.
Update:
Forgot to mention that the button calls the Application.Exit() method.
The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened.
In order to totally close a C# application, including the hidden forms, you can use the following command in the event code of the “Exit” control: Application. Exit(); Application.
Form fc = Application. OpenForms["UpdateWindow"]; if (fc != null) fc. Close(); fm.
Exit() Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
As bashmohandes and Dmitriy Matveev already mentioned the solution should be the FormClosingEventArgs. But as Dmitriy also said, this wouldn't make any difference between your button and the X in the right upper corner.
To distinguish between these two options, you can add a boolean property ExitButtonClicked to your form and set it to true in the button Click-Event right before you call Application.Exit().
Now you can ask this property within the FormClosing event and distinguish between those two options within the case UserClosing.
Example:
public bool UserClosing { get; set; } public FormMain() { InitializeComponent(); UserClosing = false; this.buttonExit.Click += new EventHandler(buttonExit_Click); this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); } void buttonExit_Click(object sender, EventArgs e) { UserClosing = true; this.Close(); } void Form1_FormClosing(object sender, FormClosingEventArgs e) { switch (e.CloseReason) { case CloseReason.ApplicationExitCall: break; case CloseReason.FormOwnerClosing: break; case CloseReason.MdiFormClosing: break; case CloseReason.None: break; case CloseReason.TaskManagerClosing: break; case CloseReason.UserClosing: if (UserClosing) { //what should happen if the user hitted the button? } else { //what should happen if the user hitted the x in the upper right corner? } break; case CloseReason.WindowsShutDown: break; default: break; } // Set it back to false, just for the case e.Cancel was set to true // and the closing was aborted. UserClosing = false; }
You can check CloseReason property of FormClosingEventArgs in FormClosing event handler to check some of the possible cases. However, cases described by you will be indistinguishable if you will only use this property. You will need to write some additional code in click event handler of your "close" button to store some information which will be checked in the FormClosing event handler to distinguish between these cases.
You need to add a listener to the Even FormClosing, which sends in the event args a property of type CloseReason which is one of these values
// Summary:
// Specifies the reason that a form was closed.
public enum CloseReason
{
// Summary:
// The cause of the closure was not defined or could not be determined.
None = 0,
//
// Summary:
// The operating system is closing all applications before shutting down.
WindowsShutDown = 1,
//
// Summary:
// The parent form of this multiple document interface (MDI) form is closing.
MdiFormClosing = 2,
//
// Summary:
// 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.
UserClosing = 3,
//
// Summary:
// The Microsoft Windows Task Manager is closing the application.
TaskManagerClosing = 4,
//
// Summary:
// The owner form is closing.
FormOwnerClosing = 5,
//
// Summary:
// The System.Windows.Forms.Application.Exit() method of the System.Windows.Forms.Application
// class was invoked.
ApplicationExitCall = 6,
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With