Form.OnClosed(EventArgs) 方法

定义

引发 Closed 事件。Raises the Closed event.

protected:
 virtual void OnClosed(EventArgs ^ e);
protected virtual void OnClosed (EventArgs e);
abstract member OnClosed : EventArgs -> unit
override this.OnClosed : EventArgs -> unit
Protected Overridable Sub OnClosed (e As EventArgs)

参数

e
EventArgs

包含事件数据的 EventArgsThe EventArgs that contains the event data.

示例

下面的示例演示如何重写从 Form 派生的类中的 OnClosed 方法。The following example demonstrates how to override the OnClosed method in a class derived from Form .

public ref class myForm: public Form
{
protected:
   virtual void OnClosed( EventArgs^ e ) override
   {
      MessageBox::Show( "The form is now closing.", "Close Warning", MessageBoxButtons::OK, MessageBoxIcon::Warning );
      Form::OnClosed( e );
   }

public:
   myForm()
      : Form()
   {}

};
public class myForm:
    Form

{
    protected override void OnClosed(EventArgs e)
    {
        MessageBox.Show("The form is now closing.", 
            "Close Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        base.OnClosed(e);
    }

    public myForm() : base()
    {        
    }

}

Public Class myForm
    Inherits Form

    Protected Overrides Sub OnClosed(ByVal e As EventArgs)
        MessageBox.Show("The form is now closing.", "Close Warning", _
            MessageBoxButtons.OK, MessageBoxIcon.Warning)
        MyBase.OnClosed(e)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

End Class

注解

注意

从 .NET Framework 2.0 开始,OnClosed 方法已过时。请改用 OnFormClosed 方法。The OnClosed method is obsolete starting with the .NET Framework 2.0; use the OnFormClosed method instead.

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

OnFormClosed 方法还允许派生类对事件进行处理而不必附加委托。The OnFormClosed 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.

继承者说明

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

适用于

另请参阅