13

I'm having trouble finding the documentation on how to add panels to a SplitContainer. I can create the SplitContainer fine, but I can't put the panels I've coded inside of the splitcontainer.

I've tried doing

sc.Container.Add(myPanel);
sc.Container.Add(myOtherPanel);

But Container is always null. Does anyone know what I'm doing wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 2,080
  • 5
  • 29
  • 44

2 Answers2

22

The SplitContainer always contains two panels and you cannot change that! (And you don't need to add any panels yourself.)

You can access the two panels through the properties Panel1 and Panel2.

If you need more panels, you can however nest several SplitContainers.


UPDATE

You cannot replace the existing panels. What you can do, is to place your own controls on the existing split container panels (and your controls can also be System.Windows.Forms.Panels containing other controls or user defined controls):

sc.Panel1.Controls.Add(myPanel);
sc.Panel2.Controls.Add(myOtherPanel);

myPanel.Dock = DockStyle.Fill;
myOtherPanel.Dock = DockStyle.Fill;

Of course you can add them using the forms designer of Visual Studio of as well, if you don't have a scenario where you have to add controls dynamically. If you create your own controls, they will automatically appear in the Toolbox inside of the same project and you can just drag and drop them on the SplitContainer's panels.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • I'm not trying to add more panels to it. I'm wanting to put the panels I've coded inside of the splitcontainer. I'll edit my post. – David May 04 '12 at 16:25
  • 1
    I see. The panels are already created and you just have to add controls to them yourself. Thanks. – David May 04 '12 at 16:32
  • Yes and you can add them using the forms designer of Visual Studio of cause, if you don't have a scenario where you have to add controls dynamically. If you create your own controls, they will automatically appear in the Toolbox inside of the same project and you can just drag and drop them on the SplitContainer's panels. – Olivier Jacot-Descombes May 04 '12 at 16:39
1

The SplitContainer control already has two panels named Panel1 and Panel2. Select the panel you want to use:

sc.Panel1.Controls.Add(myPanel);
sc.Panel2.Controls.Add(myOtherPanel);
LarsTech
  • 80,625
  • 14
  • 153
  • 225