英語で読む

次の方法で共有


Form.Activate メソッド

定義

フォームをアクティブにし、そのフォームにフォーカスを移します。

C#
public void Activate();

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

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

注釈

アクティブなアプリケーションの場合はフォームを前面に表示するか、アクティブなアプリケーションでない場合はウィンドウキャプションが点滅します。 このメソッドを有効にするには、フォームが表示されている必要があります。 アプリケーションでアクティブなフォームを確認するには、フォームが ActiveForm 複数ドキュメント インターフェイス (MDI) アプリケーションにある場合は、 プロパティまたは ActiveMdiChild プロパティを使用します。

適用対象

製品 バージョン
.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, 10

こちらもご覧ください