Click here to Skip to main content
13,351,457 members (47,383 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 27 Apr 2004

TriStateTreeview in VB.NET

, 19 Jan 2006
Rate this:
Please Sign up or sign in to vote.
A VB.NET implementation of a treeview with 3-state checkboxes using state image list


The TreeView with checkboxes implemented in the .NET Framework (property TreeView.CheckBoxes = True) allows only 2 states (TreeNode.Checked = True or False). The implementation presented here:

  • Allows to use the state image list that is not directly available in the .NET Framework implementation.

  • Allows 3 states (4 including the state none) for a node: checked, unchecked, indeterminate.

  • Implements the logic to set the proper state of parents and children when the user changes the state of a node: it selects all children when the parent is selected, and set the state of the parent(s) to checked, unchecked or indeterminate depending on the number of children selected (all, none, etc.).

I have seen other TriState implementations in the C# section but they use the image list, not the state image list, for checkboxes and therefore doesn't allow to have icons and checkboxes for nodes at the same time.

Using the code

I have derived a TriStateTreeView class from the base TreeView class. This new class needs an additional state image list with icons for the 4 states: none, unchecked, checked, indeterminate (in this order).

The state image list is passed in the constructor:

m_ctlTriStateTreeView = New TriStateTreeView(Me.StateImageList)

A new enum type is provided to specify the state of a node:

Friend Enum CheckBoxState
   None = 0
   Unchecked = 1
   Checked = 2
   Indeterminate = 3
End Enum

To add nodes to the treeview, you can use the AddTreeNode helper function of the TriStateTreeView class:

objTreeNodeRoot = m_ctlTriStateTreeView.AddTreeNode( _
   m_ctlTriStateTreeView.Nodes, "My Computer", _
   IMG_COMPUTER, CheckBoxState.None)

To get the state of a node you use the GetTreeNodeState function of the TriStateTreeView class:

Friend Function GetTreeNodeState(ByVal objTreeNode As _
   TreeNode) As CheckBoxState

To set the state of a node you use the SetTreeNodeState function of the TriStateTreeView class:

Friend Sub SetTreeNodeState(ByVal objTreeNode As _
    TreeNode, ByVal eCheckBoxState As CheckBoxState)

Points of Interest

The code intercepts the MouseUp and KeyUp events to change the state of a node (when clicking with the mouse or pressing space). Also, the code prevents expanding or collapsing a node double-clicking on the state image.


  • 27-April-2004: Initial version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...

Comments and Discussions

GeneralOnBeforeExpand & OnBeforeCollapse prevents expand collapse too much Pin
jsytniak2-Mar-09 10:55
memberjsytniak2-Mar-09 10:55 
Generalcheckbox and afterchange event Pin
iwrschenk3-Dec-08 4:18
memberiwrschenk3-Dec-08 4:18 
AnswerRe: checkbox and afterchange event Pin
jsytniak26-Dec-08 11:23
memberjsytniak26-Dec-08 11:23 
GeneralProblem with SetTreeNodeState method Pin
Omar Vargas11-Nov-08 6:50
memberOmar Vargas11-Nov-08 6:50 
NewsC# Version Pin
jsytniak7-May-08 4:37
memberjsytniak7-May-08 4:37 
Generalnode tooltip Pin
kalany16-Apr-08 7:21
memberkalany16-Apr-08 7:21 
GeneralRe: node tooltip - bug with state icons Pin
jsytniak7-May-08 4:40
memberjsytniak7-May-08 4:40 
GeneralC# version with some minor enhancements Pin
alverdal20-Mar-08 7:16
memberalverdal20-Mar-08 7:16 
GeneralRe: C# version with some minor enhancements Pin
Member 28343895-Oct-08 23:33
memberMember 28343895-Oct-08 23:33 
QuestionCheck Boxes not showing up in my project Pin
crazytom25-Jan-08 11:03
membercrazytom25-Jan-08 11:03 
GeneralRe: Check Boxes not showing up in my project Pin
crazytom27-Jan-08 9:27
membercrazytom27-Jan-08 9:27 
GeneralRe: Check Boxes not showing up in my project Pin
khendric5-Mar-08 4:32
memberkhendric5-Mar-08 4:32 
QuestionProblems with this class Pin
jonmach17-Nov-07 2:05
memberjonmach17-Nov-07 2:05 
AnswerRe: Problems with this class Pin
jonmach17-Nov-07 9:18
memberjonmach17-Nov-07 9:18 
AnswerRe: Problems with this class Pin
madmax824-Aug-10 4:02
membermadmax824-Aug-10 4:02 
1) Is seemes to be a problem with caching / memory? i finally solved this problem by disposing the form on close:

Private Sub MyForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
End Sub

When i open the form with TriStateTreeView once again, the checkboxes are there Smile | :)

I hope it helped
Generalpocket pc project Pin
rabosa21-Jun-07 0:58
memberrabosa21-Jun-07 0:58 
GeneralBeforeCheck Issue Pin
thierry_anthony13-Apr-07 8:30
memberthierry_anthony13-Apr-07 8:30 
GeneralPopule windows root [modified] Pin
mpdesign11-Feb-07 14:48
membermpdesign11-Feb-07 14:48 
QuestionTreeview state problem Pin
Achintya Jha25-Oct-06 6:49
memberAchintya Jha25-Oct-06 6:49 
AnswerRe: Treeview state problem Pin
priman_p27-Sep-09 22:31
memberpriman_p27-Sep-09 22:31 
GeneralLoadOnDemand does not work Pin
devnet24719-Feb-06 0:35
memberdevnet24719-Feb-06 0:35 
GeneralVery nice however... Pin
vbinfo31-Jan-06 5:56
membervbinfo31-Jan-06 5:56 
GeneralRe: Very nice however... Pin
Carlos J. Quintero31-Jan-06 6:17
memberCarlos J. Quintero31-Jan-06 6:17 
GeneralRe: Very nice however... Pin
devnet24713-Feb-06 13:31
memberdevnet24713-Feb-06 13:31 
GeneralRe: Very nice however... Pin
Newton Burgos5-Aug-06 17:54
memberNewton Burgos5-Aug-06 17:54 

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
Web03 | 2.8.180111.1 | Last Updated 19 Jan 2006
Article Copyright 2004 by Carlos J. Quintero
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid