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 into panels.

Split Container 
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.

Split Container
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-clicking on a control and selecting Properties menu item. The Properties window looks like Figure 3. You can also set properties of Panels by selecting a Panel and opening the Properties window.

SplitContainer Properties
Figure 3

Orientation

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

SplitContainer Orientation
Figure 4

C# Code

  1. splitContainer1.Orientation = Orientation.Horizontal;  

VB.NET Code

  1. 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

  1. SplitterPanel leftPanel = splitContainer1.Panel1;  
  2. leftPanel.BackColor = Color.Green;  
  3. leftPanel.ForeColor = Color.Yellow;  
  4. SplitterPanel rightPanel = splitContainer1.Panel2;  
  5. rightPanel.BackColor = Color.OrangeRed;  
  6. rightPanel.ForeColor = Color.White;  

VB.NET Code

  1. Dim leftPanel AsSplitterPanel = SplitContainer1.Panel1  
  2. leftPanel.BackColor = Color.Green  
  3. leftPanel.ForeColor = Color.Yellow  
  4. Dim rightPanel AsSplitterPanel = SplitContainer1.Panel2  
  5. rightPanel.BackColor = Color.OrangeRed  
  6. rightPanel.ForeColor = Color.White  

Panel1Collapsed and Panel2Collapsed

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

C# Code

  1. splitContainer1.Panel1Collapsed = true;  

VB.NET Code

  1. 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

  1. splitContainer1.Panel1MinSize = 10;  
  2. splitContainer1.Panel2MinSize = 50;  

VB.NET Code

  1. splitContainer1.Panel1MinSize = 10  
  2. 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

  1. splitContainer1.SplitterDistance = 50;  
  2. splitContainer1.SplitterIncrement = 10;  
  3. splitContainer1.SplitterWidth = 10;  

VB.NET Code

  1. splitContainer1.SplitterDistance = 50  
  2. splitContainer1.SplitterIncrement = 10  
  3. splitContainer1.SplitterWidth = 10  

Summary

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


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.