Click here to Skip to main content
Licence CPOL
First Posted 3 Jun 2005
Views 104,025
Downloads 1,695
Bookmarked 17 times

C# Control Splitcontainer

By | 3 Jun 2005 | Article
.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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Imran Rauf

Web Developer

Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmembersaberint18:03 19 Sep '11  
Questionthx a lot Imran PinmemberBitMonitor11:46 23 Aug '11  
GeneralMy vote of 2 Pinmemberjcurl1:26 13 Mar '11  
GeneralThanks Pinmemberyapple8:41 12 Aug '09  
QuestionIs possible to have one C++ unmanaged window into a C# SplitContainer control ? Pinmemberrichard0727:49 8 Jul '09  
GeneralThank you. PinmemberSuphkorn B.16:58 5 Mar '09  
GeneralLow Rating PinmemberJeffrey Scott Flesher4:50 15 Nov '05  
Questionnested splitcontainer problem ?? PinmemberHenk Meijerink2:23 12 Jun '05  
AnswerRe: nested splitcontainer problem ?? PinmemberImran Rauf22:56 16 Jun '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 3 Jun 2005
Article Copyright 2005 by Imran Rauf
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid