Programmatically adjust the splitter in a SplitContainer control in C#

adjust splitter

You can use a SplitContainer control to let the user resize two joined panels. When you make one panel larger, the other becomes smaller. The user can adjust the splitter, but you can also use code to adjust the position of the splitter.

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.

For example, the following code makes the left panel disappear.

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 previously collapsed.

The following code shows how the example program moves the splitter to 25% of the way through the SplitContainer.

private void btnLeft25_Click(object sender, EventArgs e)
    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.


This entry was posted in controls and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.