Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Control Splitcontainer

0.00/5 (No votes)
3 Jun 2005 1  
.Net Fremework 2.0 SplitContainer

Sample Image - splitcontainer.jpg

Assumption

This article assumed that you know litle about the TreView and ListView advance controll. Right now I have used Visual Studio 2005 Beta2, you can open it in Visual Studio 2003 by merging the partial classes and definately with .Net Framework 2.0.

Introduction

Windows based desktop interfaces can categorized into three types, 1) SDI-Single Document Interface like Microsoft Notepad,2) MDI-Multiple Document interface like Microsoft Word and 3)WE-Window Explorer Tree and ListView style interface. Designing a form like 'Window Explorer' is much easier now using the SplitContainer. I will demonstrate You through a simple example that how you can consume advance controll-'SplitContainer' into your application.

Description

SplitContainer is an advance window's control or it is an extended version of old splitter control. You can place it at form horizantally or vertically by specifying Orientation property each represents left/top and right/bottom panels respectively.


Properties

1) Dock

Allows you to attach Splitcontainer's border to its container. When you drop the splitcontainer on a form a Default value of Dock property is 'FILL' that means splitcontainer gets the whole area of form. In this example, I have set it to 'None' for the sack of demonstrating splitcontainer features.

2) Panel1Collapsed / Panel2Collapsed

Used to set or get whether the panel1 or Panel2 is collapsed or expanded. If you assign true to Panel1Collapsed then left or top panel will be collapsed and if you assign false to Panel1Collapsed then left or top panel will be expanded same behaviour will be occured with Panel2Collapsed property.

Following are the lines of codes which toggles the collapse and expand behaviour

/**
* Collapse left or top panel and expande right or bottom panel
*/
splitContainer1.Panel1Collapsed = true;
splitContainer1.Panel2Collapsed = false;

/**
* Collapse right or bottom panel and expande left or top panel
*/
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel1Collapsed = false;

3) Orientation

Used to set or get the orientation of splitcontainer panel

/**
* set orintation of slpitcontainer to Horizantal
*/
splitContainer1.Orientation = Orientation.Horizontal;

/**
* set orintation of slpitcontainer to Vertical
*/
splitContainer1.Orientation = Orientation.Vertical;

4) Border Style

Used to set or get the border style splitcontainer.

/**
* set border style of splitcontainer to Fixed3D 
*/
splitContainer1.BorderStyle = BorderStyle.Fixed3D;

/**
* set border style of splitcontainer to FixedSingle 
*/
splitContainer1.BorderStyle = BorderStyle.FixedSingle;

/**
* set border style of splitcontainer to None 
*/
splitContainer1.BorderStyle = BorderStyle.None;

5) Splitter Width

Splitter is a horinzantal or vertical bar which use to set panel size, default size of splitter is 4 pixel but you can set or get the splitter width value programatically by manipulating splitterwidth property.

/**
* In this example numericupdown controll is used to demonstarte that when you
* change the value of numericaupdown its changeValue apply to splitterwidth 
* property of splitcontainer
*/
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
  NumericUpDown nm = (NumericUpDown)sender;
  splitContainer1.SplitterWidth =(int) nm.Value ;
}


Events

1) SplitterMoving

This event occurs while the split control in moving state.

private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
  if(splitContainer1.Orientation == Orientation.Vertical )
    Cursor.Current = Cursors.NoMoveVert;
  else
    Cursor.Current = Cursors.NoMoveHoriz; 
}


2) SplitterMoved

This event occurs when the split control is moved.

private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
  Cursor.Current = Cursors.Default;
}

Usage

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace SplitContainerDemo
{
    public partial class MySplitContainer : Form
    {
        public MySplitContainer()
        {
            InitializeComponent();
            //Loads values to TreeView and ListView control
            this.loadValuesInto();
        }

        private void loadValuesInto()
        {
            // Create root node in TreeView
            TreeNode tnRoot = treeView1.Nodes.Add("root", "/");
            treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-1", "Node-1");
            //Finde Node in collection
            TreeNode[] nodes = treeView1.Nodes.Find("Node-1", true);
            //Assume that node will always be in TreeView then Add Childs to Node
            addChildTo(nodes[0]);
            treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-2", "Node-2");
            nodes = treeView1.Nodes.Find("Node-2", true);
            addChildTo(nodes[0]);
            treeView1.Nodes[treeView1.Nodes.IndexOfKey("root")].Nodes.Add("Node-3", "Node-3");
            nodes = treeView1.Nodes.Find("Node-3", true);
            addChildTo(nodes[0]);
        }

        //Adds childs to specific node
        private void addChildTo(TreeNode tn)
        {
            for (int i = 1; i <= 10; i++)
                tn.Nodes.Add(tn.Name + ".Child-" + i, "Child-" + i);
        }

        //Occurs when Left panel need to collapse
        private void rdoLeft_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = true;
            splitContainer1.Panel2Collapsed = false;
        }

        //Occurs when Right panel need to collapse
        private void rdoRight_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel2Collapsed = true;
            splitContainer1.Panel1Collapsed = false;
        }

        //Occurs when Defaul behaviour is required of splitcontainer
        private void rdoDefault_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = false;
            splitContainer1.Panel2Collapsed = false;
        }

        //Occurs when Horizantal orientation is required
        private void rdoHorizantal_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Orientation = Orientation.Horizontal;
        }

        //Occurs when Vertical orientation is required
        private void rdoVertical_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.Orientation = Orientation.Vertical;
        }

        //Occurs when Fixed3D border style is required
        private void rdoFix3d_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.BorderStyle = BorderStyle.Fixed3D;
        }

        //Occurs when FixedSingle border style is required
        private void rdoFixSingle_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.BorderStyle = BorderStyle.FixedSingle;
        }
        //Occurs when None border style is required
        private void rdoNone_CheckedChanged(object sender, EventArgs e)
        {
            splitContainer1.BorderStyle = BorderStyle.None;
        }

        //Occurs when TreeView node get focus, it first clears all listview items and 
        //add node's childs to listview controll
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            for (int i = 0; i < e.Node.Nodes.Count; i++)
                listView1.Items.Add(e.Node.Nodes[i].Name);
        }

        //Occurs when value is changed in NumericUpDown controll
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown nm = (NumericUpDown)sender;
            splitContainer1.SplitterWidth = (int)nm.Value;
        }

        //Occurs when splitter is moving
        private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
        {
            if (splitContainer1.Orientation == Orientation.Vertical)
                Cursor.Current = Cursors.NoMoveVert;
            else
                Cursor.Current = Cursors.NoMoveHoriz;
        }

        //Occurs when splitter has moved
        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            Cursor.Current = Cursors.Default;
        }
    }
}

Conclusion

I hope this will give you overview about splitcontainer, I really appreciate any feedback, comments and suggestion.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here