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;

事件類型

屬性

範例

下列範例示範如何使用 SetDesktopLocationClosedLoadActivatedActivate 成員。 若要執行此範例,請將下列程式碼貼到名為 的 Form1 表單中,其中包含名為 的 Button1 ,以及名為 Label1Label2 的兩 LabelButton 控制項。

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

備註

注意

.NET Framework 2.0 版中,事件 Closed 已過時;請改用 FormClosed 事件。

此事件會在使用者或 Close 表單的 方法關閉表單之後發生。 若要防止表單關閉,請處理 事件, Closing 並將傳遞給事件處理常式的 CancelEventArgs 屬性設定 Canceltrue

您可以使用這個事件來執行工作,例如釋放表單所使用的資源,以及儲存在表單中輸入的資訊,或更新其父表單。

注意

Form.Closed呼叫 方法以結束您的應用程式時, Application.Exit 不會引發 和 Form.Closing 事件。 如果您在其中一個必須執行的事件中有驗證程式代碼,您應該先個別呼叫 Form.Close 每個開啟表單的 方法,再呼叫 Exit 方法。

如果表單是 MDI 父表單, Closing 則會在引發 MDI 父表單的事件之前引發所有 MDI 子表單 Closing 的事件。 此外,所有 MDI 子表單的事件都會 Closed 在引發 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

另請參閱