Form.Closed 事件

定义

关闭窗体时发生。

public:
 event EventHandler ^ Closed;
public event EventHandler Closed;
[System.ComponentModel.Browsable(false)]
public event EventHandler Closed;
[System.ComponentModel.Browsable(false)]
public event EventHandler? Closed;
member this.Closed : EventHandler 
[<System.ComponentModel.Browsable(false)>]
member this.Closed : EventHandler 
Public Custom Event Closed As EventHandler 

事件类型

属性

示例

下面的示例演示如何使用 SetDesktopLocation、、ClosedLoadActivatedActivate 成员。 若要运行该示例,请将以下代码粘贴到名为 的Form1窗体中,Button其中包含名为 Button1 的 和两Label个名为 和 Label2Label1控件。

static int x = 200;
static int y = 200;
void Button1_Click( System::Object^ sender, System::EventArgs^ e )
{
   
   // Create a new Form1 and set its Visible property to true.
   Form1^ form2 = gcnew Form1;
   form2->Visible = true;
   
   // Set the new form's desktop location so it  
   // appears below and to the right of the current form.
   form2->SetDesktopLocation( x, y );
   x += 30;
   y += 30;
   
   // Keep the current form active by calling the Activate
   // method.
   this->Activate();
   this->Button1->Enabled = false;
}


// Updates the label text to reflect the current values of x 
// and y, which was were incremented in the Button1 control's 
// click event.
void Form1_Activated( Object^ sender, System::EventArgs^ e )
{
   Label1->Text = String::Format( "x: {0} y: {1}", x, y );
   Label2->Text = String::Format( "Number of forms currently open: {0}", count );
}

static int count = 0;
void Form1_Closed( Object^ sender, System::EventArgs^ e )
{
   count -= 1;
}

void Form1_Load( Object^ sender, System::EventArgs^ e )
{
   count += 1;
}
static int x = 200;
static int y = 200;

private void Button1_Click(System.Object sender, 
    System.EventArgs e)
{
    // Create a new Form1 and set its Visible property to true.
    Form1 form2 = new Form1();
    form2.Visible = true;

    // Set the new form's desktop location so it  
    // appears below and to the right of the current form.
    form2.SetDesktopLocation(x, y);
    x += 30;
    y += 30;

    // Keep the current form active by calling the Activate
    // method.
    this.Activate();
    this.Button1.Enabled = false;
}

// Updates the label text to reflect the current values of x 
// and y, which was were incremented in the Button1 control's 
// click event.
private void Form1_Activated(object sender, System.EventArgs e)
{
    Label1.Text = "x: "+x+" y: "+y;
    Label2.Text = "Number of forms currently open: "+count;
}

static int count = 0;

private void Form1_Closed(object sender, System.EventArgs e)
{
    count -= 1;
}

private void Form1_Load(object sender, System.EventArgs e)
{
    count += 1;
}
Shared x As Integer = 200
Shared y As Integer = 200

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Create a new Form1 and set its Visible property to true.
    Dim form2 As New Form1
    form2.Visible = True

    ' Set the new form's desktop location so it appears below and 
    ' to the right of the current form.
    form2.SetDesktopLocation(x, y)
    x += 30
    y += 30

    ' Keep the current form active by calling the Activate method.
    Me.Activate()
    Me.Button1.Enabled = False
End Sub



' Updates the label text to reflect the current values of x and y, 
' which was were incremented in the Button1 control's click event.
Private Sub Form1_Activated(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated
    Label1.Text = "x: " & x & " y: " & y
    Label2.Text = "Number of forms currently open: " & count
End Sub

Shared count As Integer = 0

Private Sub Form1_Closed(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Closed
    count -= 1
End Sub

Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    count += 1
End Sub

注解

注意

事件Closed在 .NET Framework 版本 2.0 中已过时;请改用 FormClosed 事件。

此事件在用户或 Close 窗体的 方法关闭窗体后发生。 若要防止窗体关闭,请处理 事件,Closing并将传递给事件处理程序的 的 CancelEventArgs 属性设置为 Canceltrue

可以使用此事件执行诸如释放窗体使用的资源等任务,以及保存在窗体中输入的信息或更新其父窗体。

注意

Form.Closed调用 方法退出应用程序时,Application.Exit不会引发 和 Form.Closing 事件。 如果在必须执行这两个事件中的任何一个中都有验证代码,则应在调用 Form.Close 方法之前单独为每个打开的窗体调用 Exit 方法。

如果窗体是 MDI 父窗体,则会在 Closing 引发 MDI 父窗体的事件之前引发所有 MDI 子窗体的事件 Closing 。 此外, Closed 所有 MDI 子窗体的事件都是在 MDI 父窗体的 事件引发之前 Closed 引发的。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅