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 example demonstrates the use of this member. 在此示例中,事件处理程序报告 @no__t 的事件发生。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. 若要报告多个事件或频繁发生的事件,请考虑将 @no__t 0 替换为 Console.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.

若要运行示例代码,请将其粘贴到包含名为 @no__t Form 类型实例的项目中。To run the example code, paste it into a project that contains an instance of type Form named Form1. 然后,确保事件处理程序与 @no__t 0 事件关联。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

注解

当关闭窗体时,将发生 @no__t 的事件。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. 若要取消窗体的关闭,请将传递给事件处理程序的 @no__t @no__t 的属性设置为 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. 当用户单击 "关闭" 按钮时,可以通过为窗体的 FormClosing 事件设置事件处理程序中的 DialogResult 属性,来重写分配给 DialogResult 属性的值。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.

备注

当在显示为无模式窗口的 @no__t 1 上调用 @no__t 的方法时,不能调用 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)父窗体,则在引发 MDI 父窗体的 @no__t 1 事件之前,将引发所有 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. 同样,在引发 MDI 父窗体的 @no__t 1 事件之前,将引发所有 MDI 子窗体的 @no__t 0 事件。Likewise, the FormClosed events of all MDI child forms are raised before the FormClosed event of the MDI parent form is raised. 取消 MDI 子窗体的 @no__t 0 事件不会阻止引发 MDI 父窗体的 @no__t 1 事件。Canceling the FormClosing event of an MDI child form does not prevent the FormClosing event of the MDI parent form from being raised. 但是,取消事件会将设置为 true 作为参数传递给父窗体的 FormClosingEventArgs 类的 @no__t 属性。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 属性设置为 MDI 父窗体中 falseTo 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.

适用于

另请参阅