Form.Closed 事件

定义

关闭窗体时发生。

C#
public event EventHandler Closed;
C#
[System.ComponentModel.Browsable(false)]
public event EventHandler Closed;
C#
[System.ComponentModel.Browsable(false)]
public event EventHandler? Closed;

事件类型

属性

示例

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

C#
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;
}

注解

注意

事件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 引发的。

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

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅