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 

イベントの種類

属性

次の例では、、ClosedActivatedLoadおよび Activate の各メンバーをSetDesktopLocation使用する方法を示します。 この例を実行するには、次のコードを という名前のフォームに貼り付けます。このフォームには、 という名前Button1の コントロールと と という Label1Label2Form1 2 つのLabelコントロールが含Buttonまれています。

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 処理し、イベント ハンドラーに Cancel 渡された の CancelEventArgs プロパティを に true設定します。

このイベントを使用すると、フォームで使用されるリソースの解放などのタスクを実行したり、フォームに入力された情報を保存したり、親フォームを更新したりできます。

注意事項

Form.Closedおよび Form.Closing イベントは、アプリケーションを終了するために メソッドがApplication.Exit呼び出されたときに発生しません。 実行する必要があるこれらのイベントのいずれかに検証コードがある場合は、 メソッドを Form.Close 呼び出す前に、開いているフォームごとに メソッドを Exit 個別に呼び出す必要があります。

フォームが MDI 親フォームの場合、 Closing MDI 親フォームのイベントが発生する前に、すべての MDI 子フォームの Closing イベントが発生します。 さらに、 Closed MDI 親フォームのイベントが発生する前に、 Closed すべての MDI 子フォームのイベントが発生します。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください