Form.Closing 事件

在关闭窗体时发生。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Event Closing As CancelEventHandler
用法
Dim instance As Form
Dim handler As CancelEventHandler

AddHandler instance.Closing, handler
public event CancelEventHandler Closing
public:
event CancelEventHandler^ Closing {
    void add (CancelEventHandler^ value);
    void remove (CancelEventHandler^ value);
}
/** @event */
public void add_Closing (CancelEventHandler value)

/** @event */
public void remove_Closing (CancelEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。

备注

警告

在 .NET Framework 版本 2.0 中,Closing 事件已过时,请改用 FormClosing 事件。

在窗体关闭时,Closing 事件发生。在窗体关闭后,将释放在该对象内创建的所有资源并释放该窗体。如果取消此事件,则该窗体保持打开状态。若要取消窗体的关闭操作,请将传递给事件处理程序的 CancelEventArgsCancel 属性设置为 true

当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)会隐藏窗体并将 DialogResult 属性设置为 DialogResult.Cancel。通过在窗体的 Closing 事件的事件处理程序中设置 DialogResult 属性,可以重写用户单击“关闭”按钮时分配给 DialogResult 属性的值。

提示

在显示为无模式窗口的 Form 上调用 Close 方法时,不能调用 Show 方法使窗体可见,因为窗体的资源已被释放。若要隐藏窗体然后又使其可见,请使用 Control.Hide 方法。

警告

当调用 Application.Exit 方法以退出应用程序时,不引发 Form.ClosedForm.Closing 事件。如果在必须执行的其中一个事件中有验证代码,则在调用 Exit 方法之前,应分别为每个打开的窗体调用 Form.Close 方法。

如果窗体是 MDI 父窗体,则在引发 MDI 父窗体的 Closing 事件之前将引发所有 MDI 子窗体的 Closing 事件。另外,在引发 MDI 父窗体的 Closed 事件之前,将引发所有 MDI 子窗体的 Closed 事件。取消 MDI 子窗体的 Closing 事件不能防止引发 MDI 父窗体的 Closing 事件。但是,取消该事件会将作为参数传递给父窗体的 CancelEventArgsCancel 属性设置成 true。要强制关闭所有 MDI 父窗体和子窗体,请将 MDI 父窗体中的 Cancel 属性设置成 true

有关处理事件的更多信息,请参见 使用事件

示例

   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 'Form1_Closing
End Class 'Form1
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 void Form1Closing(Object sender,
    System.ComponentModel.CancelEventArgs e)
{
    // Determine if text has changed in the textbox by comparing to 
    // original text.
    if (textBox1.get_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).Equals(
            get_DialogResult().Yes)) {
            // Cancel the Closing event from closing the form.
            e.set_Cancel(true);
            // Call method to save file...
        }
    }
} //Form1Closing 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Form 类
Form 成员
System.Windows.Forms 命名空间
Form.IsMdiContainer 属性
OnClosing