Form.Closing Event
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Caution
Form.OnClosing, Form.OnClosed and the corresponding events are obsolete. Use Form.OnFormClosing, Form.OnFormClosed, Form.FormClosing and Form.FormClosed instead.
Occurs when the form is closing.
public:
event System::ComponentModel::CancelEventHandler ^ Closing;
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
[System.Obsolete("Form.OnClosing, Form.OnClosed and the corresponding events are obsolete. Use Form.OnFormClosing, Form.OnFormClosed, Form.FormClosing and Form.FormClosed instead.", false, DiagnosticId="WFDEV004", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public event System.ComponentModel.CancelEventHandler? Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler? Closing;
member this.Closing : System.ComponentModel.CancelEventHandler
[<System.ComponentModel.Browsable(false)>]
member this.Closing : System.ComponentModel.CancelEventHandler
[<System.ComponentModel.Browsable(false)>]
[<System.Obsolete("Form.OnClosing, Form.OnClosed and the corresponding events are obsolete. Use Form.OnFormClosing, Form.OnFormClosed, Form.FormClosing and Form.FormClosed instead.", false, DiagnosticId="WFDEV004", UrlFormat="https://aka.ms/winforms-warnings/{0}")>]
member this.Closing : System.ComponentModel.CancelEventHandler
Public Custom Event Closing As CancelEventHandler
Event Type
- Attributes
Remarks
Caution
The Closing event is obsolete; 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.
Note
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
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 false
in the MDI parent form.
For more information about handling events, see Handling and Raising Events.