如何在MDI父表单旁边的另一个子表单上使用按钮打开子表单

时间:2019-02-14 22:45:54

标签: c# winforms mdi

我有3个表单MainForm,Child1和Child2。 MainForm是mdi容器。在运行时,子项1将自动加载到MainForm中。我正在尝试通过child1上的按钮打开child2表单,但我似乎无法做到。我已经尽力了。请帮忙。

我尝试使用hide show和activate方法,但没有任何效果。 child1和child2表单是分别构建的。

public partial class Form1 : Form
    {
        public child1 c1;
        public child2 c2;

        public void LoadChild2()
        {
            if (c2 == null)
            {
                c2 = new child2() ;
                c2.MdiParent = this;
                c2.FormClosed += c2_FormClosed;
                c2.FormBorderStyle = FormBorderStyle.None;
                c2.Dock = DockStyle.Fill;
                c2.Show();
            }
            else { c2.Activate(); }


        }

        void c2_FormClosed(object sender, FormClosedEventArgs e)
        {
            c2 = null;

        }
        public void LoadChild1()
        {
            if (c1 == null)
            {

                c1 = new child1();
                c1.MdiParent = this;
                c1.FormClosed += c1_FormClosed;
                c1.FormBorderStyle = FormBorderStyle.None;
                c1.Dock = DockStyle.Fill;
                c1.Show();
            }
            else { c1.Activate(); }

        }

        void c1_FormClosed(object sender, FormClosedEventArgs e)
        {
            c1 = null;
            //throw new NotImplementedException();
        }
        public Form1()
        {
            InitializeComponent();
            LoadChild1();

           // c2 = new child2();
          //  c1 = new child1();

       // c2.MdiParent = this;
       // c1.MdiParent = this;
        //c1.WindowState = FormWindowState.Maximized;
       // c1.FormBorderStyle = FormBorderStyle.None;
        //c1.Dock = DockStyle.Fill;
       // c2.FormBorderStyle = FormBorderStyle.None;
        //c2.Dock = DockStyle.Fill;
       // c2.Show();
       // c1.Show();

        //c1.Activate(); 
    }

     public partial class child1 : Form
{
    //public child2 c2;

    public child1()
    {
        InitializeComponent();   
    }
    private void child1_Load(object sender, EventArgs e)
    {

    }
    private void button1_Click(object sender, EventArgs e)
    {          
        Form1 f = new Form1();      
        f.LoadChild2();
        //child2 c2 = new child2();
        //Form1 f = new Form1();
        //c2.FormBorderStyle = FormBorderStyle.None;
       // c2.Dock = DockStyle.Fill;
        //c2.MdiParent = f;
       // c2.Activate();
       // c2.BringToFront();
       // this.Hide();
        //this.Close();
    }
}

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

    private void button1_Click(object sender, EventArgs e)
    {

        //this.Close();
        Form1 f = new Form1();
        f.LoadChild1();
       // child1 c1 = new child1();
       // c1.FormBorderStyle = FormBorderStyle.None;
       // c1.Dock = DockStyle.Fill;

       // c1.Parent = f;
       // c1.Show();
       // c1.Activate();

    }

    private void child2_Load(object sender, EventArgs e)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

确保设置MdiParent属性;如果从现有MdiChild上的按钮的Click事件处理程序中生成新表单,则只需将其设置为该孩子的MdiParent:

// Event handler inside MdiChild form (Child1)
private void spawnNewChildButton_Click(object sender, EventArgs e)
{
    // Child2 is a System.Windows.Forms.Form
    var child2 = new Child2() { MdiParent = MdiParent };
    child2.Show();
}