C# Splitter Example

Learn the Splitter Component in C# using these simple stepby step examples.

This tutorial will help you learn the following concepts:

  1. How to use Splitter in C# windows forms.
  2. How to seperate several WinForms Panels with a Splitter in C#.

Example 1: Simple C# Splitter Example.

A simple example in C# to help you learn C#.

This example will comprise the following files:

  • Splitter.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). Splitter.cs

Create a file named Splitter.cs

Add two imports:

using System;
using System.Windows.Forms;

namespace Examples {


 You will need to create a Form that will host our components including the Splitter component.

```csharp
  class MainForm : Form {

Define our main method as below:

    public static void Main() {

Enable the Visual Stylesof the application, then invoke the Run as shown below:

      Application.EnableVisualStyles();
      Application.Run(new MainForm());

Declare the properties of the MainForm inside a constructor:

    public MainForm() {

We will proceed to initialize the properties of our Form as well as components like Panel and Splitter:

      Text = "Splitter example";
      ClientSize = new System.Drawing.Size(300, 300);

      splitterTop.Parent = this;
      splitterTop.Dock = DockStyle.Top;

      panelTop.Parent = this;
      panelTop.Dock = DockStyle.Top;
      panelTop.Height = 150;
      panelTop.BackColor = System.Drawing.Color.LightPink;

We will declare these as instance fields of our class:

    private Splitter splitterTop = new Splitter();
    private Panel panelTop = new Panel();
    private Splitter splitterLeft = new Splitter();
    private Panel panelLeft = new Panel();
    private Splitter splitterRight = new Splitter();
    private Panel panelRight = new Panel();
    private Panel panelFill = new Panel();

Here is the full code

using System;
using System.Windows.Forms;
namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "Splitter example";
      ClientSize = new System.Drawing.Size(300, 300);

      splitterTop.Parent = this;
      splitterTop.Dock = DockStyle.Top;

      panelTop.Parent = this;
      panelTop.Dock = DockStyle.Top;
      panelTop.Height = 150;
      panelTop.BackColor = System.Drawing.Color.LightPink;
      splitterLeft.Parent = this;
      splitterLeft.Dock = DockStyle.Left;

      panelLeft.Parent = this;
      panelLeft.Dock = DockStyle.Left;
      panelLeft.Width = 70;
      panelLeft.BackColor = System.Drawing.Color.LightBlue;

      splitterRight.Parent = this;
      splitterRight.Dock = DockStyle.Right;

      panelRight.Parent = this;
      panelRight.Dock = DockStyle.Right;
      panelRight.Width = 70;
      panelRight.BackColor = System.Drawing.Color.LightYellow;

      panelFill.Parent = this;
      panelFill.Dock = DockStyle.Fill;
      panelFill.BackColor = System.Drawing.Color.LightGreen;
    }
    private Splitter splitterTop = new Splitter();
    private Panel panelTop = new Panel();
    private Splitter splitterLeft = new Splitter();
    private Panel panelLeft = new Panel();
    private Splitter splitterRight = new Splitter();
    private Panel panelRight = new Panel();
    private Panel panelFill = new Panel();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts