在另一个表单上打开新表单

时间:2015-01-10 06:27:30

标签: c# winforms

我的主窗体有几个按钮,每个按钮打开新窗体。当我单击button1时,form1打开,当我单击button2时,form2打开但form1返回到主窗体。我想要一些功能,以便每个新表单在父表单上打开,最新表单在顶部打开。

这是我的代码

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 form = new Form1();
        form.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        form.Show();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form3 form = new Form3();
        form.Show();
    }

1 个答案:

答案 0 :(得分:2)

每个表单都有一个最顶层的属性,只需将它们设置为true

private void button1_Click(object sender, EventArgs e)
    {
        Form1 form = new Form1();
        form.Show();
        form.TopMost = true;
        form.Activate();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 form = new Form2();
        form.Show();
        form.TopMost = true;
        form.Activate();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form3 form = new Form3();
        form.Show();
        form.TopMost = true;
        form.Activate();
    }