Form.FormClosing Form.FormClosing Form.FormClosing Form.FormClosing Event

定义

关闭窗体前发生。Occurs before the form is closed.

public:
 event System::Windows::Forms::FormClosingEventHandler ^ FormClosing;
public event System.Windows.Forms.FormClosingEventHandler FormClosing;
member this.FormClosing : System.Windows.Forms.FormClosingEventHandler 
Public Custom Event FormClosing As FormClosingEventHandler 

示例

下面的代码示例演示如何将此成员。The following code example demonstrates the use of this member. 在示例中,事件处理程序报告的匹配项FormClosing事件。In the example, an event handler reports on the occurrence of the FormClosing event. 此报告可帮助您了解事件发生的并可以帮助您进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告或频繁发生的事件上多个事件,请考虑更换MessageBox.ShowConsole.WriteLine或将消息附加到多行TextBoxTo report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行此代码示例,请将其粘贴到包含类型的实例的项目Form名为Form1To run the example code, paste it into a project that contains an instance of type Form named Form1. 然后,确保事件处理程序相关联FormClosing事件。Then ensure that the event handler is associated with the FormClosing event.

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event" );
}
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
     Handles Form1.FormClosing

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Cancel", e.Cancel)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"FormClosing Event")

End Sub

注解

FormClosing事件会出现在窗体正在关闭。The 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. 若要取消窗体的闭包,请设置Cancel的属性FormClosingEventArgs传递给事件处理程序trueTo cancel the closure of a form, set the Cancel property of the FormClosingEventArgs passed to your event handler to true.

当窗体显示为模式对话框中时,单击关闭按钮 (与窗体的右上角的 X 按钮) 后,窗体会被隐藏,DialogResult属性设置为DialogResult.CancelWhen 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. 可以重写的值分配给DialogResult属性在用户单击时关闭通过设置按钮DialogResult中的事件处理程序属性FormClosing窗体的事件。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 FormClosing event of the form.

备注

Close上调用方法Form显示为无模式窗口,则不能调用Show方法使窗体可见,因为已释放窗体的资源。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. 若要隐藏窗体,然后将其可见,请使用Hide方法。To hide a form and then make it visible, use the Hide method.

如果窗体是多文档界面 (MDI) 父窗体FormClosingMDI 父窗体之前引发事件的所有 MDI 子窗体FormClosing引发事件。If the form is a multiple-document interface (MDI) parent form, the FormClosing events of all MDI child forms are raised before the MDI parent form's FormClosing event is raised. 同样,FormClosed之前,将引发事件的所有 MDI 子窗体FormClosed引发 MDI 父窗体。Likewise, the FormClosed events of all MDI child forms are raised before the FormClosed event of the MDI parent form is raised. 正在取消FormClosingMDI 子窗体的事件不会阻止FormClosing引发 MDI 父窗体的事件。Canceling the FormClosing event of an MDI child form does not prevent the FormClosing event of the MDI parent form from being raised. 但是,取消该事件将设置为trueCancel属性的FormClosingEventArgs作为参数传递到父窗体的类。However, canceling the event will set to true the Cancel property of the FormClosingEventArgs class that is passed as a parameter to the parent form. 若要强制所有 MDI 父和子窗体关闭,请设置Cancel属性设置为false在 MDI 父窗体。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.

适用于

另请参阅