0

在.Net WinForms 我有两种形式。Form1 已打开且处于活动状态。如何打开新表单 (Form2):

  • Form1 必须有焦点
  • Form2在Form1下打开(后台Form1)

第一点很简单,请参阅stackoverflow ... 但我不知道如何在Form1下显示Form2。谢谢。

编辑

 public partial class Form1 : Form
{
    Form2 frm;

    public Form1()
    {
        InitializeComponent();
        frm = new Form2();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Focus();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.AddOwnedForm(frm);
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

}

表格2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();            
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Focus();
    }

    protected override bool ShowWithoutActivation
    {
        get
        {
            return true;
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            //make sure Top Most property on form is set to false
            //otherwise this doesn't work
            int WS_EX_TOPMOST = 0x00000008;
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= WS_EX_TOPMOST;
            return cp;
        }
    }
4

3 回答 3

2

我认为第二点你需要做的是:

var form2 = new Form2();
    form2.MdiParent = form1; //form1 is your parent form; use this operator in case you are creating this form from out base form

    form2.WindowState = FormWindowState.Normal;
    form2.Show();

请查看此以获取更多详细信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.mdiparent.aspx

编辑

请参阅下面的代码,该代码显示 frm 在 Form1 之上打开。

public partial class Form1 : Form
{
    Form2 frm;

    public Form1()
    {
        InitializeComponent();
        frm = new Form2();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Maximized;
        textBox1.Focus();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (!frm.Visible)
        {
          this.AddOwnedForm(frm);
          frm.Show();
          frm.SendToBack();
          this.BringToFront();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.AddOwnedForm(frm);
        frm.Show();
        frm.SendToBack();
        this.BringToFront();
    }

}
于 2012-10-03T05:03:26.877 回答
0

您只需实例化 Form2 并显示它,例如:

var form2 = new Form2();
form2.Visible = true;

然后将 Form1 设置为活动表单(假设这是在 Form1 的代码中完成的 - 因此是this):

this.BringToFront();
于 2012-10-03T04:47:40.463 回答
0

我猜你想要一个对话

var form2 = new Form2(); 
form2.Visible = true; 
this.ShowDialog();
于 2012-10-03T05:47:37.797 回答