C# SplitContainer

SplitContainer presents a resizable view. It splits the layout of your Windows Forms window into two parts: a left and right half, or a top and bottom half. It gives the user more control over how the window is arranged.

SplitContainer control

This C# article shows the SplitContainer control from Windows Forms. It provides example code.

Get started

Steps

To begin, take your Windows Forms application and add the SplitContainer control to it. You can do this by double-clicking on the SplitContainer icon in the Toolbox. By default, the SplitContainer has an orientation of vertical, indicating a vertical split; you can change this property (see below).

SplitContainer vertical resizing

Two panels. The SplitContainer will have two subcontainers in it. They can be accessed through the Panel1 and Panel2 properties (see below). You can drag controls, such as a Button control or a TextBox control, to the two panels. To make a control expand in the panel, please use the Anchor property.

Anchor in Windows Forms

Panel1 and Panel2 properties

Property (Icon copyright Microsoft)

You can add or remove controls, or change existing controls, by using the Panel1 and Panel2 properties in C# code. In this example, we enumerate all the controls in Panel1 and Panel2 for the program shown in the screenshots above. The results indicate that a Button instance is found in Panel1 and a TextBox instance is found in Panel2.

Example that uses Panel1 and Panel2 on SplitContainer [C#]

using System;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    // Get information about Panel1 controls and Panel2 controls.
	    StringBuilder builder = new StringBuilder();
	    foreach (var control in splitContainer1.Panel1.Controls)
	    {
		builder.Append("Panel1: ").AppendLine(control.GetType().ToString());
	    }
	    foreach (var control in splitContainer1.Panel2.Controls)
	    {
		builder.Append("Panel2: ").AppendLine(control.GetType().ToString());
	    }
	    MessageBox.Show(builder.ToString());
	}
    }
}

Result

Panel1: System.Windows.Forms.Button
Panel2: System.Windows.Forms.TextBox

SplitterMoved event handler

Lightning bolt

One useful event handler on the SplitContainer control is the SplitterMoved event handler. This is triggered when the move has already occurred, not when it starts or is happening. If you have some reason to readjust parts of your program, or want to persist the position of the Splitter to disk, you can listen for this event.

Orientation property

The orientation property of the SplitContainer is very useful: it allows you to change from vertical splitting to horizontal splitting. For the Horizontal setting, you can move the splitter up and down with the mouse.

Summary

.NET Framework information

The SplitContainer control in Windows Forms is a simple but useful container widget that allows you to present a resizable, divided view to the user. It can have a vertical or horizontal split, and it has two properties Panel1 and Panel2 that can contain collections of subcontrols.

Windows Forms
.NET