【问题标题】:How to show(Bringtofront) an already opened Mdi child form from another Mdi child form?如何从另一个 Mdi 子窗体显示(Bringtofront)已打开的 Mdi 子窗体?
【发布时间】:2014-10-28 20:49:43
【问题描述】:

在我的应用程序中,我有 4 个表单,form1 是一个 mdi 容器,其余表单是子容器, 在 form1 中,我在其加载事件中打开所有子表单。在子form2中我有一个按钮将切换到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();

    }

【问题讨论】:

  • 如果我在按钮中初始化新实例,它将在每次按钮单击时创建新的表单实例。
  • 让一个孩子意识到另一个孩子是一种非常不确定的做法。将此代码放在 MDI 父窗体中,它必然已经知道您的所有子窗体。只需从孩子那里调用它,例如 ((MyMDIParent)this.MdiParent).FooBar()。
  • 我没有得到你的解释请多发一些代码谢谢

标签: c# mdi mdichild


【解决方案1】:

当你创建你的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();

【讨论】:

    【解决方案2】:
    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();
        }
    

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      相关资源
      最近更新 更多