SplitContainer in C#

The SplitContainer control provides the functionality of a splitter to divide and resize two controls. In this article, we will discuss how to create and use a SplitContainer control in a Windows Forms application.

Creating a SplitContainer

A SplitContainer has two built-in panels and a splitter. If you drag a SplitContainer control from Toolbox onto a Form, you will see output like Figure 1, where a splitter is separating Panel1 and Panel2 panels.

SplitContainerImg1.jpg 

Figure 1

Now you can place any child control in Panel1 and Panel2 and you will be able to resize them. Let's drag a TreeView control onto Panel1 and a ListBox control onto Panel2 and set their Dock property to true. The output looks like Figure 2. If you run this application, you will see a resizing option available between the TreeView and ListBox controls.

SplitContainerImg2.jpg

Figure 2

SplitContainer Properties

After you place a SplitContainer control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open the Properties window by pressing F4 or right-click on a control and select Properties menu item. The Properties window looks like Figure 3. You can also set properties of Panels by selecting a Panel and open the Properties window.

SplitContainerImg3.jpg

Figure 3

Orientation

Panels on a SplitContainer can be placed horizontally or vertically. The horizontal orientation places Panels like Figure 4. 

SplitContainerImg4.jpg

Figure 4

C# Code:

splitContainer1.Orientation = Orientation.Horizontal;

 

VB.NET Code:

 

splitContainer1.Orientation = Orientation.Horizontal

Panel1 and Panel2

A SplitContainer has two panels. The first panel is represented by Panel1 and the second panel is represented by Panel2. These panels are a type of SplitterPanel and can have their own properties and events.

C# Code:

SplitterPanel leftPanel = splitContainer1.Panel1;

leftPanel.BackColor = Color.Green;

leftPanel.ForeColor = Color.Yellow;           

 

SplitterPanel rightPanel = splitContainer1.Panel2;

rightPanel.BackColor = Color.OrangeRed;

rightPanel.ForeColor = Color.White;

 

VB.NET Code:

 

Dim leftPanel As SplitterPanel = SplitContainer1.Panel1

leftPanel.BackColor = Color.Green

leftPanel.ForeColor = Color.Yellow

 

Dim rightPanel As SplitterPanel = SplitContainer1.Panel2

rightPanel.BackColor = Color.OrangeRed

rightPanel.ForeColor = Color.White

Panel1Collapsed and Panel2Collapsed

Panel1Collapsed and Panel2Collapsed properties can be used to collapse and expand panels.

C# Code:

splitContainer1.Panel1Collapsed = true;

 

VB.NET Code:

 

splitContainer1.Panel1Collapsed = True

Panel1MinSize and Panel2MinSize

Panel1MinSize property gets or sets the minimum distance in pixels of the splitter from the left or top edge of Panel1. The Panel2MinSize property gets or sets the minimum distance in pixels of the splitter from the right or bottom edge of Panel2.

C# Code:

splitContainer1.Panel1MinSize = 10;

splitContainer1.Panel2MinSize = 50;

 

VB.NET Code:

 

splitContainer1.Panel1MinSize = 10

splitContainer1.Panel2MinSize = 50

Splitter Properties

The SplitterDistance property gets or sets the location of the splitter, in pixels, from the left or top edge of the SplitContainer.

The SplitterIncrement property gets or sets a value representing the increment of splitter movement in pixels.

The SplitterRectangle property gets the size and location of the splitter relative to the SplitContainer.

The SplitterWidth property Gets or sets the width of the splitter in pixels.

C# Code:

splitContainer1.SplitterDistance = 50;

splitContainer1.SplitterIncrement = 10;

splitContainer1.SplitterWidth = 10;

 

VB.NET Code:

 

splitContainer1.SplitterDistance = 50

splitContainer1.SplitterIncrement = 10

splitContainer1.SplitterWidth = 10

 

Summary

In this article, we discussed use of a SplitContainer control and use of various properties of it.

Up Next
    Ebook Download
    View all
    Learn
    View all