如何从另一个Mdi子窗体显示(Bringtofront)已打开的Mdi子表单
本文关键字:Mdi 表单 Bringtofront 显示 另一个 窗体 | 更新日期: 2023-09-27 18:20:32
在我的应用程序中,我有4个表单,form1是一个mdi容器,其余的表单是它的子级,在form1中,我在加载事件中打开所有子窗体。在childform2中,我有一个将切换到childform3的按钮。我的问题是如何从childform2 中的按钮显示childform3(已经打开)
form1:
Form2 formchild1;
Form3 formchild2;
Form4 formchild3;
private void Form1_Load(object sender, EventArgs e)
{
if (formchild2 == null)
{
formchild2 = new Form3();
}
formchild2.MdiParent = this;
formchild2.Dock = DockStyle.Fill;
formchild2.Show();
//formchild2.BringToFront();
if (formchild3 == null)
{
formchild3 = new Form4();
}
formchild3.MdiParent = this;
formchild3.Dock = DockStyle.Fill;
formchild3.Show();
if (formchild1 == null)
{
formchild1 = new Form2();
}
formchild1.MdiParent = this;
formchild1.Show();
formchild1.Dock = DockStyle.Fill;
formchild1.BringToFront();
}
form2:
Form3 formchild2;
private void button1_Click(object sender, EventArgs e)
{
//what i have to write hare..
//formchild2 = new Form3();
//formchild2.MdiParent = this.ParentForm;
//formchild2.Dock = DockStyle.Fill;
//formchild2.Show();
//formchild2.BringToFront();
}
当您创建form2时(请更改变量名,花了一段时间才发现formchild1实际上是form2),您需要实例化form2实例
if (formchild1 == null)
{
formchild1 = new Form2(/*Either pass in a Form3 here*/);
}
formchild1.formChild2 = formchild2; //Or make formChild2 public member
formchlid1.SetForm(formChild2); //Or make a method that sets it
formchild1.MdiParent = this;
formchild1.Show();
formchild1.Dock = DockStyle.Fill;
formchild1.BringToFront();
然后再次显示,你可以只做
formchild2.BringToFront();
childform3 childform3=new childform3;
private void button1_Click(object sender, EventArgs e)
{
if (!childform3.IsDisposed)
childform3.Select();
else
childform3= new frmSearch();
childform3.MdiParent = ParentForm;
childform3.Show();
}