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.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show(e.CloseReason.ToString());
}

The CloseReason can be one of:

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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.