46

I want to hide panel2 on a split container and have panel1 utilize the space. I was hoping setting Panel2Collapsed would do the trick, but no luck. Ideas?

1
  • 1
    What actually happens when you set Panel2Collapsed to true? Does Panel2 stay visible? Commented Mar 14, 2009 at 7:02

6 Answers 6

87

This worked for me on a similar situation:

splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Hide();

I wanted the second panel to not be visible at all in some cases, so I implemented it this way.

9
  • If you don't set the SplitterDistance and calling Hide(), which part of Panel2 is visible? Commented Mar 14, 2009 at 6:48
  • 1
    If I recall correctly, if you didn't set the SplitterDistance, then at the far end of Panel1 the mouse cursor would change, making it evident that there is another panel there. Commented Mar 14, 2009 at 6:52
  • but what happens when the user resizes the form
    – CrashCodes
    Commented Mar 14, 2009 at 7:00
  • @Nikos: Hmm... I can't recreate that :-/ That kind of issue exists with the Splitter control, but I thought the whole purpose of the SplitContainer was to do away with those issues... Commented Mar 14, 2009 at 7:01
  • @CrashCodes: If the splitcontainer is docked, or properly anchored it will resize with the form, and Panel1 will stay docked to fill the entire splitcontainer. Commented Mar 14, 2009 at 7:10
3

Setting Panel2Collapsed property to true in the form designer and programatically both work as you want them to (i.e. Panel1 then occupies all of the space)... so there must be something else going on.

1
            this.splitContainerControl1.Panel2.Hide();
            this.splitContainerControl1.Panel2.Height = 0;
            this.splitContainerControl1.IsSplitterFixed = true; 

This worked for me.

0
1

splitContainer1.PanelVisibility = SplitPanelVisibility.Panel1

2
  • No such property exists. Commented Feb 10, 2014 at 5:43
  • It works for Devexpress SplitContainerControl. Maybe here is not right place but it worked for me, thanks. Commented Feb 1, 2016 at 9:37
1

With Visual Studio 2017 it is a little more trick. This is what I got to work for me. MyControl is inside panel1.

'vb.net:

MySplitContainer.Panel2Collapsed = True
MySplitContainer.Panel2.Hide()
MySplitContainer.SplitterDistance = MySplitContainer.Height
MySplitContainer.Panel1.Anchor = AnchorStyles.Bottom
MyControl.Height = MySplitContainer.Height

'for C# just add a semi-colon onto the end of each line and it should work.
-3

Try setting panel2.Visible = false.

Not the answer you're looking for? Browse other questions tagged or ask your own question.