System.Windows.Forms.Form.FormClosing Event

Occurs before the form is closed.

Syntax

public event FormClosingEventHandler FormClosing

Remarks

The Form.FormClosing event occurs as the form is being closed. When a form is closed, it is disposed, releasing all resources associated with the form. If you cancel this event, the form remains opened. To cancel the closure of a form, set the System.ComponentModel.CancelEventArgs.Cancel property of the System.Windows.Forms.FormClosingEventArgs 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 Form.DialogResult property to be set to DialogResult.Cancel. You can override the value assigned to the Form.DialogResult property when the user clicks the Close button by setting the Form.DialogResult property in an event handler for the Form.FormClosing event of the form.

Note:

When the Form.Close method is called on a System.Windows.Forms.Form displayed as a modeless window, you cannot call the Control.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.

If the form is a multiple-document interface (MDI) parent form, the Form.FormClosing events of all MDI child forms are raised before the MDI parent form's Form.FormClosing event is raised. Likewise, the Form.FormClosed events of all MDI child forms are raised before the Form.FormClosed event of the MDI parent form is raised. Canceling the Form.FormClosing event of an MDI child form does not prevent the Form.FormClosing event of the MDI parent form from being raised. However, canceling the event will set to true the System.ComponentModel.CancelEventArgs.Cancel property of the System.Windows.Forms.FormClosingEventArgs class that is passed as a parameter to the parent form. To force all MDI parent and child forms to close, set the System.ComponentModel.CancelEventArgs.Cancel property to false in the MDI parent form.

For more information about handling events, see Consuming Events.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0