Form.Closing 事件

定义

关闭窗体时发生。Occurs when the form is closing.

public:
 event System::ComponentModel::CancelEventHandler ^ Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler Closing;
member this.Closing : System.ComponentModel.CancelEventHandler 
Public Custom Event Closing As CancelEventHandler 
属性

示例

下面的示例使用 Closing 测试 TextBox 中的文本是否已更改。The following example uses Closing to test if the text in a TextBox has changed. 如果有,则会询问用户是否保存对文件所做的更改。If it has, the user is asked whether to save the changes to a file.

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 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 Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
      ' Determine if text has changed in the textbox by comparing to original text.
      If textBox1.Text <> strMyOriginalText Then
         ' 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 Then
            ' Cancel the Closing event from closing the form.
            e.Cancel = True
         End If ' Call method to save file...
      End If
   End Sub
End Class

注解

注意

从 .NET Framework 2.0 开始,Closing 事件已过时:请改用 FormClosing 事件。The Closing event is obsolete starting with the .NET Framework 2.0; use the FormClosing event instead.

关闭窗体时发生 Closing 事件。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. 若要取消窗体的关闭,请将传递到事件处理程序的 CancelEventArgsCancel 属性设置为 "true"。To cancel the closure of a form, set the Cancel property of the CancelEventArgs 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. 当用户单击 "关闭" 按钮时,可以通过为窗体的 Closing 事件设置事件处理程序中的 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 Closing event of the form.

备注

当对显示为无模式窗口的 Form 调用 Close 方法时,不能调用 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. 若要隐藏窗体并使其可见,请使用 Control.Hide 方法。To hide a form and then make it visible, use the Control.Hide method.

注意

调用 Application.Exit 方法退出应用程序时,不会引发 Form.ClosedForm.Closing 事件。The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. 如果在必须执行的任何一个事件中都有验证代码,则应在调用 Exit 方法之前,分别为每个打开的窗体调用 Form.Close 方法。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.

如果窗体为 MDI 父窗体,则在引发 MDI 父窗体的 Closing 事件之前,将引发所有 MDI 子窗体的 Closing 事件。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. 此外,在引发 MDI 父窗体的 Closed 事件之前,将引发所有 MDI 子窗体的 Closed 事件。In addition, the Closed events of all MDI child forms are raised before the Closed event of the MDI parent form is raised. 取消 MDI 子窗体的 Closing 事件不会阻止引发 MDI 父窗体的 Closing 事件。Canceling the Closing event of an MDI child form does not prevent the Closing event of the MDI parent form from being raised. 但是,取消事件会将设置为 true 作为参数传递给父窗体的 CancelEventArgsCancel 属性。However, canceling the event will set to true the Cancel property of the CancelEventArgs 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.

适用于

另请参阅