Form.OnClosing(CancelEventArgs) 方法

定义

引发 Closing 事件。Raises the Closing event.

protected:
 virtual void OnClosing(System::ComponentModel::CancelEventArgs ^ e);
protected virtual void OnClosing (System.ComponentModel.CancelEventArgs e);
abstract member OnClosing : System.ComponentModel.CancelEventArgs -> unit
override this.OnClosing : System.ComponentModel.CancelEventArgs -> unit
Protected Overridable Sub OnClosing (e As CancelEventArgs)

参数

e
CancelEventArgs

包含事件数据的 CancelEventArgsA CancelEventArgs that contains the event data.

示例

下面的示例使用 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 开始,OnClosing 方法已过时。请改用 OnFormClosing 方法。The OnClosing method is obsolete starting with the .NET Framework 2.0; use the OnFormClosing method instead.

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息,请参阅处理和引发事件For more information, see Handling and Raising Events.

OnClosing 方法还允许派生类对事件进行处理而不必附加委托。The OnClosing method also allows derived classes to handle the event without attaching a delegate. 重写此方法是在派生类中处理事件的首选技术。Overriding this method is the preferred technique for handling the event in a derived class.

注意

如果调用了 Application.Exit 方法来退出应用程序,则不会调用 OnClosedOnClosing 方法。The OnClosed and OnClosing methods are not called when the Application.Exit method is called to exit your application. 如果必须执行这些方法中的任何一个,则应在调用 Exit 方法之前,分别为每个打开的窗体调用 Form.Close 方法。If you have validation code in either of these methods that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

继承者说明

在派生类中重写 OnClosing(CancelEventArgs) 时,一定要调用基类的 OnClosing(CancelEventArgs) 方法,以便已注册的委托对事件进行接收。When overriding OnClosing(CancelEventArgs) in a derived class, be sure to call the base class's OnClosing(CancelEventArgs) method so that registered delegates receive the event.

适用于

另请参阅