Search code examples
c#winformsmdimdichild

Adding a form to a splitcontainer from a child form


I'm trying to add a form to a SplitContainer from a child form. I can do it from the forms parent using this.

Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = this;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
this.splitContainer.Panel2.Controls.Add(form);
form.Show();

But I can't figure out how to do it from a child of the parent form. Any help is appreciated.


Solution

  • Here's how I solved the problem. I passed a reference to the child form.

            MessageBoxRegister register = new MessageBoxRegister(this);
            register.ShowDialog();
    

    I then saved the reference in a global variable.

        Launcher launcher;
        public MessageBoxRegister(Launcher launcher)
        {
            InitializeComponent();
    
            this.launcher = launcher;
        }
    

    Then I could open the form into the splitContainer like this.

                Lockdown.MainForm form = new Lockdown.MainForm(true);
                form.MdiParent = launcher;
                form.TopLevel = false;
                form.Dock = DockStyle.Fill;
                launcher.splitContainer.Panel2.Controls.Add(form);
                form.Show();