Click here to Skip to main content
15,035,723 members
Articles / Programming Languages / C#
Posted 28 Mar 2004


73 bookmarked

Tri-State TreeView Control

Rate me:
Please Sign up or sign in to vote.
4.42/5 (28 votes)
2 Nov 2005CPL2 min read
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

Comments and Discussions

QuestionThis Post is very helpul for me Pin
ashishgupta12125-Mar-13 8:46
Memberashishgupta12125-Mar-13 8:46 
QuestionGracias Pin
jc2211871-Mar-13 15:08
Memberjc2211871-Mar-13 15:08 
QuestionNot Displaying in Form Pin
akhilrajau14-Dec-12 16:37
Memberakhilrajau14-Dec-12 16:37 
QuestionThank you! Pin
krillgar2-Mar-12 11:32
Memberkrillgar2-Mar-12 11:32 
Questionmuy bueno Pin
Member 772271318-Nov-11 10:50
MemberMember 772271318-Nov-11 10:50 
BugRight To Left Pin
nagham_4ng31-Jul-11 23:10
Membernagham_4ng31-Jul-11 23:10 
GeneralRe: Right To Left Pin
Ebse17-Aug-11 5:20
MemberEbse17-Aug-11 5:20 
GeneralMy vote of 5 Pin
vstetsko7-Mar-11 9:13
Membervstetsko7-Mar-11 9:13 
QuestionUnusual Behavior Pin
Phantom2081-Feb-11 6:35
MemberPhantom2081-Feb-11 6:35 
AnswerRe: Unusual Behavior Pin
Phantom2082-Feb-11 5:55
MemberPhantom2082-Feb-11 5:55 
QuestionHow to use this dll in 64 bit mechines Pin
Member 329057315-Sep-10 16:18
MemberMember 329057315-Sep-10 16:18 
AnswerRe: How to use this dll in 64 bit mechines Pin
Cody Tang7-Oct-10 23:30
professionalCody Tang7-Oct-10 23:30 
GeneralRe: How to use this dll in 64 bit mechines Pin
Member 223057026-Dec-10 12:11
MemberMember 223057026-Dec-10 12:11 
GeneralRe: How to use this dll in 64 bit mechines Pin
Cody Tang30-Jul-11 21:49
professionalCody Tang30-Jul-11 21:49 
GeneralRe: How to use this dll in 64 bit mechines Pin
pman30-Jul-11 1:01
Memberpman30-Jul-11 1:01 
GeneralImage problem Pin
Devon Peterson17-Feb-10 12:53
MemberDevon Peterson17-Feb-10 12:53 
GeneralRe: Image problem Pin
Devon Peterson17-Feb-10 13:20
MemberDevon Peterson17-Feb-10 13:20 
GeneralAdded ability to have disabled nodes. You might find this usefull. [modified] Pin
David Catriel17-Jun-09 2:49
MemberDavid Catriel17-Jun-09 2:49 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
giova12-Oct-09 22:53
Membergiova12-Oct-09 22:53 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
David Catriel13-Oct-09 5:07
MemberDavid Catriel13-Oct-09 5:07 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
David Catriel13-Oct-09 6:07
MemberDavid Catriel13-Oct-09 6:07 
GeneralAbsolutely awesome Pin
David Catriel26-May-09 3:48
MemberDavid Catriel26-May-09 3:48 
Generalcontains node! Pin
njuniorba7-May-09 15:07
Membernjuniorba7-May-09 15:07 
GeneralDisable Node Pin
kjward20-Apr-09 4:53
Memberkjward20-Apr-09 4:53 
GeneralRe: Disable Node Pin
David Catriel10-Jun-09 8:59
MemberDavid Catriel10-Jun-09 8:59 

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.