Learn why a form is closing in C#
The form's FormClosing event handler receives a parameter e that has property CloseReason that tells you why the form is closing. The following code displays the reason to the user:
// Tell the user why the form is closing.The CloseReason can be one of:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show(e.CloseReason.ToString());
}
| Name | Meaning |
|---|---|
| None | Unknown reason. |
| WindowsShutDown | Windows is shutting down. |
| MdiFormClosing | The parent of this MDI form is closing. |
| UserClosing | The user is closing the form by clicking the close (X) button, using the form's system menu, or pressing Alt-F4. This is also the reason if the code executes the form's Close method. |
| TaskManagerClosing | The Task Manager is killing the form. |
| FormOwnerClosing | The owner form is closing. |
| ApplicationExitCall | The program called Application.Exit. |



Comments