Click here to Skip to main content
11,798,866 members (46,619 online)
Click here to Skip to main content

Tri-State TreeView Control

, 2 Nov 2005 CPL 279.2K 6.1K 69
Rate this:
Please Sign up or sign in to vote.
A TreeView control with tri-state checkboxes.


Microsoft provides a TreeView control in the .NET framework, but as usual it doesn't expose all the functionality that the underlying common control provides. It has the possibility to display check-boxes but they can't be tri-state check boxes.

This control adds tri-state check boxes and the necessary handling methods to check/uncheck all sub-items if the user clicks on a node, as well as displays a grey-checked check box for the parent if the siblings have differing check/uncheck states.


A tri-state TreeView is a tree view control that displays check boxes next to the tree nodes. If you click on a check box, it checks (or unchecks) the check box and all sub-nodes.

If the nodes on the same level as the clicked node have differing check states the parent node's check box is grey-checked as well as its parent's and so on.

If you click on a grey-checked check box it unchecks that node and all its subnodes.

How to use the control

Add TriStateTreeView.cs to your project and then simply drop the TriStateTreeView control on a form and use it as a regular TreeView control. The only difference is when you want to access the checked state of a node. Instead of using the treeNode.Checked property you call triStateTreeView.GetChecked(treeNode) resp. triStateTreeView.SetChecked(treeNode, checkState). Instead of a bool these methods take a CheckState.

For example, the following code snippet checks the state of all the top level nodes:

foreach (TreeNode node in m_treeView.Nodes)
  if (m_treeView.GetChecked(node) == 

How does it work?

The control derives from TreeView and sets the ImageList and SelectedImageList properties for the base control. It overrides the OnClick and OnKeyDown methods.

In the OnKeyDown method, we check if the pressed key is a space, and if it is we change the state of the selected node.

The OnClick method is a little trickier. We have to use the TVM_HITTEST Win32 API message to determine if the user clicked on the icon or on the item. If the user clicked on the icon we change the state of the selected node.


This control doesn't support displaying both check boxes and icons. If you need this functionality you have to call additional Win32 API methods. The common control tree view control supports having multiple image lists, but this functionality isn't exposed in .NET.


The control makes use of the Skybound.VisualStyle assembly from By commenting out the lines that reference that assembly it will easily work without it. Another way of making the control aware of visual styles is described here.

The demo project consists of an assembly that contains the TriStateTreeView, a demo project and a NUnit test assembly with some tests for the control.


  • 26 March 2004 - First version.
  • 28 October 2005 - Added support for visual styles and Before/AfterCheck.


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: check box can't be displayed Pin
CodeForFun20-Sep-07 7:49
memberCodeForFun20-Sep-07 7:49 
GeneralRe: check box can't be displayed Pin
zz_lsl20-Dec-10 3:40
memberzz_lsl20-Dec-10 3:40 
GeneralOnBeforeCheck [modified] Pin
mjelten19-Jul-07 22:23
membermjelten19-Jul-07 22:23 
QuestionReset all to unchecked? Pin
Jim Baldwin24-Jun-07 19:30
memberJim Baldwin24-Jun-07 19:30 
QuestionHow to add multiple images on same tree node? Pin
sushantya6-Mar-07 19:39
membersushantya6-Mar-07 19:39 
GeneralTri-State TreeView in Pocket Project Pin
Silas Pereira27-Feb-07 1:08
memberSilas Pereira27-Feb-07 1:08 
QuestionHow do I get what is checked? Pin
JoppeG26-Feb-07 21:33
memberJoppeG26-Feb-07 21:33 
AnswerArrayList Bug? Pin
JoppeG28-Feb-07 0:55
memberJoppeG28-Feb-07 0:55 
I tried to do a count command on the ArrayList but it replays "0" no matter how it is checked.
ArrayList ChkList = new ArrayList();
ChkList = triStateTreeView1.GetCheckedTagData();


Seems like there is a problem with the code that should count the checked alternatives. Or?
GeneralSolved problem Pin
JoppeG28-Feb-07 4:07
memberJoppeG28-Feb-07 4:07 
GeneralRe: Solved problem Pin
jonmach18-Nov-07 7:38
memberjonmach18-Nov-07 7:38 
QuestionRight-To-Left Problem Pin
morali2k2-Sep-06 2:58
membermorali2k2-Sep-06 2:58 
GeneralSimplified tree-state TreeView Pin
Laurent Muller22-Aug-06 4:35
memberLaurent Muller22-Aug-06 4:35 
GeneralRe: Simplified tree-state TreeView Pin
UncleRedz1-Nov-06 4:18
memberUncleRedz1-Nov-06 4:18 
GeneralRe: Simplified tree-state TreeView Pin
barclay19-Dec-06 11:44
memberbarclay19-Dec-06 11:44 
GeneralRe: Simplified tree-state TreeView Pin
hamlet_13-Feb-07 0:38
memberhamlet_13-Feb-07 0:38 
GeneralRe: Simplified tree-state TreeView Pin
Martin Welker4-Jun-07 12:17
memberMartin Welker4-Jun-07 12:17 
GeneralRe: Simplified tree-state TreeView Pin
cwalex24-Jul-07 11:26
membercwalex24-Jul-07 11:26 
GeneralRe: Simplified tree-state TreeView Pin
MAEI28-Sep-07 8:25
memberMAEI28-Sep-07 8:25 
GeneralRe: Simplified tree-state TreeView Pin
Laurent Muller1-Oct-07 4:08
memberLaurent Muller1-Oct-07 4:08 
GeneralRe: Simplified tree-state TreeView (do not set Checkboxes to True) Pin
Dial@Technet30-Oct-07 4:30
memberDial@Technet30-Oct-07 4:30 
GeneralRe: Simplified tree-state TreeView Pin
jcachat16-Nov-07 6:55
memberjcachat16-Nov-07 6:55 
GeneralRe: Simplified tree-state TreeView Pin
luisxvarg11-Jan-10 23:38
memberluisxvarg11-Jan-10 23:38 
GeneralSuggestion about using StateImageList Pin
lukasz_sw21-Feb-06 12:06
memberlukasz_sw21-Feb-06 12:06 
GeneralImplementing an icon Pin
devnet24730-Jan-06 19:54
memberdevnet24730-Jan-06 19:54 
GeneralHelp to use this control in a form! Pin
srf23-Jan-06 20:39
membersrf23-Jan-06 20:39 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 2 Nov 2005
Article Copyright 2004 by Ebse
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid