Click here to Skip to main content
13,346,487 members (128,772 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 18 Feb 2010

Simple Tri-State TreeView

, 30 Mar 2011
Rate this:
Please Sign up or sign in to vote.
Provides another, simple way to get a tri-state TreeView, ensuring compatibility to all Windows UIs / styles


Microsoft provides a TreeView control which isn't able to display tree state checkboxes. There are many ways to implement this feature such as window proc hooking and the other stuff. This article will show you another, more simpler way using techniques provided by the .NET Framework only.

This control only inherits the original tree view control replacing CheckBoxes property usage with usage of the StateImageList and primarily overriding the node click event procedure to handle setting the tri-state.
To conform to all Windows UI styles, the state image list will be built dynamically using the CheckBoxRenderer class provided by the .NET Framework while the control is created.


There may be needs to have a tree view control showing on parent nodes that check states its children aren't all the same. Further there may be needs, refreshing check states up / down the tree while checking a sub-/super-node.

Using techniques such as window proc hooking isn't the right way for everyone because it isn't always easy to debug. Further using static images would avoid displaying right checkboxes on different Windows UIs.

Using the Control / Code

Add the TriStateCheckBoxTreeView.cs to your project and simply drop the control to a form. You'll find the differences to the normal tree view control provided by the .NET Framework in the property grid. There is now a property to enable tri-state usage directly below the CheckBoxes-property.


Check state checking is done as normal using the Checked property from the tree node. To determine all three possible states, you may check the StateImageIndex giving the following three possible indexes:

  • 0 - Unchecked
  • 1 - Checked
  • 2 - Mixed

Rendering checkboxes using CheckBoxRenderer

Bitmap GetCheckBoxBitmap(CheckBoxState myState)
    Bitmap bmpCheckBox = new Bitmap(16, 16);
    Graphics gfxCheckBox = Graphics.FromImage(bmpCheckBox);
    CheckBoxRenderer.DrawCheckBox(gfxCheckBox, new Point(2, 2), myState);
    return bmpCheckBox;

Setting the tri-state

protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
 Stack<TreeNode> stNodes;
 TreeNode tnBuffer;
 bool bMixedState;
 int iSpacing;
 int iIndex;
    iSpacing = ImageList == null ? 0 : 18;			// if user clicked area
    if (e.X > e.Node.Bounds.Left - iSpacing ||		// *not* used by the state
        e.X < e.Node.Bounds.Left - (iSpacing + 16))		// image we can leave here.
    { return; }
    tnBuffer = e.Node;					// buffer clicked node and
    tnBuffer.Checked = !tnBuffer.Checked;			// flip its check state.
    stNodes = new Stack<TreeNode>(tnBuffer.Nodes.Count);	// create a new stack and
    stNodes.Push(tnBuffer);					// push buffered node first.
    do {							// let's pop node from stack,
        tnBuffer = stNodes.Pop();				// inherit buffered node's
	tnBuffer.Checked = e.Node.Checked;			// check state and push
        for (int i = 0; i < tnBuffer.Nodes.Count; i++)		// each child on the stack
            stNodes.Push(tnBuffer.Nodes[i]);			// until there is no node
    } while (stNodes.Count > 0);				// left.
    bMixedState = false;
    tnBuffer = e.Node;					// re-buffer clicked node.
    while (tnBuffer.Parent != null) {			// while we get a parent we
        foreach (TreeNode tnChild in tnBuffer.Parent.Nodes)	// determine mixed check states
            bMixedState |= (tnChild.Checked != tnBuffer.Checked);// and convert current check
        iIndex = (int)Convert.ToUInt32(tnBuffer.Checked);	// state to state image index.
        tnBuffer.Parent.Checked = bMixedState || (iIndex > 0);	// set parent's check state and
        if (bMixedState)					// state image in dependency
            tnBuffer.Parent.StateImageIndex = CheckBoxesTriState ? 2 : 1;
        else						// of mixed state.
            tnBuffer.Parent.StateImageIndex = iIndex;
        tnBuffer = tnBuffer.Parent;				// finally buffer parent and
    }							// loop here.


This code has been written as simple as possible. There is one limitation you should keep in mind: If you're adding nodes from code you have to call control's Refresh() after adding has been completed to ensure all nodes got the right state.


  • 17 February 2010: First version
  • 18 February 2010: Extended sections "Introduction", "Background", "Using the control / code", fixed demo project
  • 18 March 2010: Small cosmetics to comments
  • 30 March 2011: Updated demo project


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
csmanul17-Mar-16 0:30
membercsmanul17-Mar-16 0:30 
BugCannot disable check and sometimes parent update status incorrectly Pin
Frank Shaw9-Dec-15 16:40
memberFrank Shaw9-Dec-15 16:40 
QuestionNot Uncheck Parent Node in certain condition Pin
Thiago Pires de Araujo12-Nov-15 23:32
memberThiago Pires de Araujo12-Nov-15 23:32 
General5 stars Pin
mors74117-Mar-15 23:53
membermors74117-Mar-15 23:53 
Question5 star ! Pin
Matan Fihman17-Dec-14 3:39
memberMatan Fihman17-Dec-14 3:39 
GeneralMy vote of 5 Pin
dangler235-Jun-13 18:16
memberdangler235-Jun-13 18:16 
QuestionASP.NET Pin
rohith11923-Sep-12 21:22
memberrohith11923-Sep-12 21:22 
QuestionProblem with RTL check in checkBox Pin
efirozen17-Jun-12 20:06
memberefirozen17-Jun-12 20:06 
QuestionAlternative version submitted Pin
Fred_Informatix5-Mar-12 23:12
memberFred_Informatix5-Mar-12 23:12 
GeneralMy vote of 5 Pin
Dreamcooled26-Feb-12 6:15
memberDreamcooled26-Feb-12 6:15 
QuestionHow can I invert the checkstates for this treeview? Pin
StijnBollen20-Jan-12 0:40
memberStijnBollen20-Jan-12 0:40 
QuestionBug: Unchecking a mixed state node does not update parents mixed state Pin
oyvdahl5-Dec-11 3:15
memberoyvdahl5-Dec-11 3:15 
BugRefresh has no effect Pin
Member 824238016-Sep-11 1:24
memberMember 824238016-Sep-11 1:24 
AnswerRe: Refresh has no effect Pin
Member 824238023-Sep-11 5:10
memberMember 824238023-Sep-11 5:10 
BugStateImageIndex issue Pin
dymarski5-Jul-11 8:26
memberdymarski5-Jul-11 8:26 
GeneralRe: StateImageIndex issue Pin
Iznogood11-Sep-11 1:18
memberIznogood11-Sep-11 1:18 
SuggestionHow just to hide checkbox of the certain TreeNode in TreeView control Pin
Member 14778530-Jun-11 17:16
memberMember 14778530-Jun-11 17:16 
GeneralRe: How just to hide checkbox of the certain TreeNode in TreeView control Pin
DreamCatcher2k1030-Jun-11 22:34
memberDreamCatcher2k1030-Jun-11 22:34 
GeneralSmall issue Pin
Member 793518429-May-11 8:59
memberMember 793518429-May-11 8:59 
AnswerRe: Small issue Pin
DreamCatcher2k1029-May-11 23:33
memberDreamCatcher2k1029-May-11 23:33 
GeneralRe: Small issue Pin
TheBertster30-May-11 2:07
memberTheBertster30-May-11 2:07 
GeneralProblem deselecting node - update parent node Pin
veydanvario30-Mar-11 23:57
memberveydanvario30-Mar-11 23:57 
AnswerRe: Problem deselecting node - update parent node Pin
DreamCatcher2k1031-Mar-11 3:05
memberDreamCatcher2k1031-Mar-11 3:05 
GeneralRe: Problem deselecting node - update parent node Pin
veydanvario1-Apr-11 0:29
memberveydanvario1-Apr-11 0:29 
GeneralRe: Problem deselecting node - update parent node Pin
Mauro Luque1-Apr-11 10:52
memberMauro Luque1-Apr-11 10:52 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 30 Mar 2011
Article Copyright 2010 by DreamCatcher2k10
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid