-1

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Hmya, hopefully you don't think that MdiParent means anything. You need a reference to the SplitContainer object. How you get it is entirely up to you. You can raise an event so the parent can do it. Or you can call a method of the parent, requires the child to know its parent. Mine do, call me daddy, I passed it to the constructor. Or you can declare a public static variable so anybody can do it. – Hans Passant Dec 23 '15 at 23:05
  • I tried putting the above code in a method in the parent form, and calling the method from the child form. The method runs, but the called form never appears. it only works if called from the parent form. – Shayne T. Thiessen Dec 24 '15 at 00:00

1 Answers1

0

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();