共用方式為


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 

事件類型

屬性

範例

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

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

備註

警告

.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 引發。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱