Click here to Skip to main content
Click here to Skip to main content

Tri-State TreeView Control

, 2 Nov 2005 CPL
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

Comments and Discussions

QuestionThis Post is very helpul for me Pinmemberashishgupta12125-Mar-13 9:46 
QuestionGracias Pinmemberjc2211871-Mar-13 16:08 
QuestionNot Displaying in Form Pinmemberpodal14-Dec-12 17:37 
QuestionThank you! Pinmemberkrillgar2-Mar-12 12:32 
Questionmuy bueno PinmemberMember 772271318-Nov-11 11:50 
BugRight To Left Pinmembernagham_4ng1-Aug-11 0:10 
GeneralRe: Right To Left PinmemberEbse17-Aug-11 6:20 
GeneralMy vote of 5 Pinmembervstetsko7-Mar-11 10:13 
QuestionUnusual Behavior PinmemberPhantom2081-Feb-11 7:35 
I use a subroutine to fill the treeview by looping through parent and child records in a SQLite database. It works fine when the db is opened during form_load but if the db is built after the form loads, the tableadapters are filled and then the same subroutine is run, the treeview scroll bars are visible but the nodes are not. I have stepped through the code in debug mode and know that tvRooms.Nodes.Count > 0 and the subroutine does not terminate early. Has anyone else seen this behavior and, if so, what can I do to fix it? Here is the subroutine:
    Private Sub FillTreeView()
            With tvRooms
                For Each drRoom As DataRow In HouseholdInventoryDataSet.Tables("Rooms").Rows
                    'Adds the room as a root node
                    Dim nodParent As TreeNode
                    nodParent = tvRooms.Nodes.Add(drRoom.Item("Room").ToString)
                    nodParent.Tag = drRoom.Item("Room").ToString
                    For Each drItem As DataRow In drRoom.GetChildRows(HouseholdInventoryDataSet.Relations("Rooms_Inventory"))
                        'Adds the inventory item as child of the room
                        Dim nodChild As TreeNode
                        nodChild = nodParent.Nodes.Add(drItem.Item("ItemName").ToString)
                        nodChild.Tag = drItem.Item("ID")
                    Next drItem
                Next drRoom
                If tvRooms.Nodes.Count = 0 Then Exit Sub
            End With
        Catch ex As Exception
            gMsg = "The following error occurred filling the tree view: " + vbCrLf + ex.Message
        End Try
    End Sub

AnswerRe: Unusual Behavior PinmemberPhantom2082-Feb-11 6:55 
QuestionHow to use this dll in 64 bit mechines PinmemberMember 329057315-Sep-10 17:18 
AnswerRe: How to use this dll in 64 bit mechines PinmemberCody Tang8-Oct-10 0:30 
GeneralRe: How to use this dll in 64 bit mechines PinmemberMember 223057026-Dec-10 13:11 
GeneralRe: How to use this dll in 64 bit mechines PinmemberCody Tang30-Jul-11 22:49 
GeneralRe: How to use this dll in 64 bit mechines Pinmemberpman30-Jul-11 2:01 
GeneralImage problem PinmemberDevon Peterson17-Feb-10 13:53 
GeneralRe: Image problem PinmemberDevon Peterson17-Feb-10 14:20 
GeneralAdded ability to have disabled nodes. You might find this usefull. [modified] PinmemberMuaddubby17-Jun-09 3:49 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pinmembergiova12-Oct-09 23:53 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. PinmemberMuaddubby13-Oct-09 6:07 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. PinmemberMuaddubby13-Oct-09 7:07 
GeneralAbsolutely awesome PinmemberMuaddubby26-May-09 4:48 
Generalcontains node! Pinmembernjuniorba7-May-09 16:07 
GeneralDisable Node Pinmemberkjward20-Apr-09 5:53 
GeneralRe: Disable Node PinmemberMuaddubby10-Jun-09 9:59 

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
Web03 | 2.8.141220.1 | Last Updated 2 Nov 2005
Article Copyright 2004 by Ebse
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid