Export (0) Print
Expand All

Form.Closing Event

Occurs when the form is closing.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public event CancelEventHandler Closing
/** @event */
public void add_Closing (CancelEventHandler value)

/** @event */
public void remove_Closing (CancelEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

Caution noteCaution

The Closing event is obsolete in the .NET Framework version 2.0; use the FormClosing event instead.

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. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the Closing event of the form.

NoteNote

When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the Control.Hide method.

Caution noteCaution

The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

If the form is an MDI parent form, the Closing events of all MDI child forms are raised before the MDI parent form's Closing event is raised. In addition, the Closed events of all MDI child forms are raised before the Closed event of the MDI parent form is raised. Canceling the Closing event of an MDI child form does not prevent the Closing event of the MDI parent form from being raised. However, canceling the event will set to true the Cancel property of the CancelEventArgs that is passed as a parameter to the parent form. To force all MDI parent and child forms to close, set the Cancel property to true in the MDI parent form.

For more information about handling events, see Consuming Events.

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // Determine if text has changed in the textbox by comparing to original text.
   if (textBox1.Text != strMyOriginalText)
   {
      // Display a MsgBox asking the user to save changes or abort.
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   }
}

private void Form1Closing(Object sender,
    System.ComponentModel.CancelEventArgs e)
{
    // Determine if text has changed in the textbox by comparing to 
    // original text.
    if (textBox1.get_Text() != strMyOriginalText) {
        // Display a MsgBox asking the user to save changes or abort.
        if (MessageBox.Show("Do you want to save changes to your text?",
            "My Application", MessageBoxButtons.YesNo).Equals(
            get_DialogResult().Yes)) {
            // Cancel the Closing event from closing the form.
            e.set_Cancel(true);
            // Call method to save file...
        }
    }
} //Form1Closing 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft