打开每个窗体作为MdiParent的子窗体

本文关键字:窗体 MdiParent | 更新日期: 2023-09-27 18:07:06

我有MdiParent形式有MenuSubmenu父窗体是说窗体A如果我打开窗体B使用子菜单选项使用以下代码

B addbill = new B();
B.Show();
B.MdiParent = this;

打开表单B,作为表单A的子窗体。现在我想从表格B中打开表格C,点击表格B上的按钮后,表格B将被关闭,表格C将作为表格A的子窗体打开。再次点击表格C按钮后,表格C将关闭,表格B将打开为表格A

我该怎么做呢?

打开每个窗体作为MdiParent的子窗体

在FormB按钮上单击事件编写以下代码:

 FormC fc=new FormC();
 fc.MdiParent=this.MdiParent;
 fc.Show();

在FormC加载事件中写如下代码:

FormB fb=new FormB();
fb.Hide();
fb.Close();

在B关闭前:

C.MdiParent = B.MdiParent; // which is pointing to A

在窗体B按钮中单击调用窗体C的位置,您必须将FormB的MdiParent分配给FormC的MdiParent,即FormA。之后可以关闭FormB。

//表单按钮点击

private void button1_Click(object sender, EventArgs e)
        {
            FormC frm = new FormC();
            frm.MdiParent = this.MdiParent; // assign MdiParent of FormB to FormC
            frm.Show();
            this.Close();
        }
    private void button1_Click(object sender, EventArgs e)
    {
        Analysis an = new Analysis();//on login click open anothe form on same perrent
        an.MdiParent = this.MdiParent;
        an.Show();

    }

It's Work. Try This Code

 private void btCountSale_Click(object sender, EventArgs e)
    {
        bool exist = false;
        foreach (Form f in Application.OpenForms)
        {
            if (f.Text == "Counter Sale")
            {
                exist = true;
                f.BringToFront();
                break;
            }
        }
        if (exist == false)
        {
            frmCounterSale fm = new frmCounterSale();
            fm.MdiParent = this.MdiParent; 
            fm.Show();
        }
    }

It's Work. Try This Code

 private void btCountSale_Click(object sender, EventArgs e)
    {
        bool exist = false;
        foreach (Form f in Application.OpenForms)
        {
            if (f.Name== "frmCounterSale")
            {
                exist = true;
                f.BringToFront();
                break;
            }
        }
        if (exist == false)
        {
            frmCounterSale fm = new frmCounterSale();
            fm.MdiParent = this.MdiParent; 
            fm.Show();
        }
    }