Programmatically adjust the position of the splitter in a SplitContainer control in C#

You can use the SplitContainer's Panel1Collapsed and Panel2Collapsed properties to collapse one of the panels and make it disappear. While a panel is collapsed, the user cannot adjust the splitter.

The following code makes the left panel disappear in a horizontal SplitContainer.

    SplitContainer1.Panel1Collapsed = true;

To set the splitter to a particular position, set the SplitContainer's SplitterDistance property. Be sure to un-collapse any panels that you may have collapsed.

The following code moves the splitter to 25% of the way through the SplitContainer.

    SplitContainer1.Panel1Collapsed = false;
SplitContainer1.Panel2Collapsed = false;
SplitContainer1.SplitterDistance = (int)(SplitContainer1.ClientSize.Width * 0.25);

In the example program, click the buttons to collapse a panel or move the splitter to 25%, 50%, or 75% of the way through the SplitContainer.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.