Click here to Skip to main content
15,902,889 members
Articles / Programming Languages / C#
Article

C# Control Splitcontainer

Rate me:
Please Sign up or sign in to vote.
2.80/5 (28 votes)
3 Jun 2005CPOL2 min read 199.1K   5.5K   24   12
.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

C#
/**
* 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

C#
/**
* 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.

C#
/**
* 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.

C#
/**
* 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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow expand or colaps with timer Pin
Beni Khaeroni20-Oct-15 16:02
Beni Khaeroni20-Oct-15 16:02 
Questiontreeview control Pin
durgeshradhe11-Sep-15 0:27
durgeshradhe11-Sep-15 0:27 
GeneralMy vote of 3 Pin
karthik.pvm8-Oct-12 0:43
karthik.pvm8-Oct-12 0:43 
GeneralMy vote of 1 Pin
saberint19-Sep-11 18:03
saberint19-Sep-11 18:03 
Questionthx a lot Imran Pin
BitMonitor23-Aug-11 11:46
BitMonitor23-Aug-11 11:46 
GeneralMy vote of 2 Pin
Jason Curl13-Mar-11 1:26
professionalJason Curl13-Mar-11 1:26 
GeneralThanks Pin
yapple12-Aug-09 8:41
yapple12-Aug-09 8:41 
QuestionIs possible to have one C++ unmanaged window into a C# SplitContainer control ? Pin
richard0728-Jul-09 7:49
richard0728-Jul-09 7:49 
GeneralThank you. Pin
Suphkorn B.5-Mar-09 16:58
Suphkorn B.5-Mar-09 16:58 
GeneralLow Rating Pin
Jeffrey Scott Flesher15-Nov-05 4:50
Jeffrey Scott Flesher15-Nov-05 4:50 
Questionnested splitcontainer problem ?? Pin
Henk Meijerink12-Jun-05 2:23
Henk Meijerink12-Jun-05 2:23 
Imran,

Many thanks for your article. Have you come across (or can you explain) the puzzling behaviour below?

I tried nesting SplitContainers in a windows form in vs.net 2005 beta 2 and ran into an unexpected behaviour. One of the SplitContainer panels (the outer top panel) only appears to auto-resize if I stretch the form vertically or diagonally. Stretching the form horizontally only appears to resize the outer lower panel. Vertically stretching the form resizes all panels.

The panel that does resize okay (the outer lower panel) is fixed and has a nested SplitContainer (vertical orientation) the panels of which have several controls all of which are anchored Top and Left, and not docked.

The other top panel and its nested SplitContainer panels all contain only one RichTextBox control each. These RichTextBox controls (unlike the other controls above) are docked to fill.

The outer top panel is not fixed and has a nested SplitContainer (horizontal orientation), the upper panel of which has yet another nested SplitContainer (horizontal orientation).

All SplitContainers are docked to fill.

Regards, Henk.
AnswerRe: nested splitcontainer problem ?? Pin
Imran Rauf16-Jun-05 22:56
Imran Rauf16-Jun-05 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.