Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / Visual Basic
Article

TriStateTreeview in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.12/5 (14 votes)
19 Jan 20062 min read 180.8K   2.3K   40   39
A VB.NET implementation of a treeview with 3-state checkboxes using state image list

Introduction

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:

VB.NET
m_ctlTriStateTreeView = New TriStateTreeView(Me.StateImageList)

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

VB.NET
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:


VB.NET
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:


VB.NET
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:


VB.NET
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.

History

  • 27-April-2004: Initial version.

License

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



Comments and Discussions

 
GeneralRe: Very nice however... Pin
devnet24713-Feb-06 12:31
devnet24713-Feb-06 12:31 
GeneralRe: Very nice however... Pin
Newton Burgos5-Aug-06 16:54
Newton Burgos5-Aug-06 16:54 
GeneralGreat Job Pin
Paul Conrad24-Jan-06 16:54
professionalPaul Conrad24-Jan-06 16:54 
GeneralMissing exe/dll Pin
Jonas Beckeman18-Jan-06 11:17
Jonas Beckeman18-Jan-06 11:17 
GeneralRe: Missing exe/dll Pin
Carlos J. Quintero18-Jan-06 22:24
Carlos J. Quintero18-Jan-06 22:24 
GeneralGreat !! Pin
jeromesubs18-Aug-05 5:04
jeromesubs18-Aug-05 5:04 
Questionhow to do it in asp.net? Pin
steven_wong23-May-05 16:42
steven_wong23-May-05 16:42 
GeneralExcellent - but needed C++! Pin
skonopa28-Feb-05 9:34
skonopa28-Feb-05 9:34 
Excellent job and nice bit of coding. Unfortunatly, I needed something like this in C++.

I ended up spending the better part of a day porting this over to Managed C++, and got it to work quit nicely there as well (with the default State images internalized itto the TriStateTreeView class. The user can supply an optional set by setting a "StateImage" property after "new"ing an instance of the control.

I've seen all the ways to "fake it" using the Icon images, but was nice to finally find one that actually got ahold of the actual checkbox image and update it as necessary (which is exactly what I wanted, as I was not really impressed with any of the "fake it" versions).

The big mystery is why the heck did MS not supply something like this as part of the framework in the first place? I've seen some of thier own friggan programs make use of a tri-state type tree, such as the back-up program that came with Windows 2000.

Again, an excellent job on this. I can only imagine what a pain it must've been to figure out all the internal messaging and the like to get ahold of, as well as set, those state images and the states.
GeneralRe: Excellent - but needed C++! Pin
skonopa1-Mar-05 12:42
skonopa1-Mar-05 12:42 
GeneralRe: Excellent - but needed C++! Pin
Atlantys20-May-05 8:49
Atlantys20-May-05 8:49 
GeneralExpand and Check problem Pin
Patrick Mc.30-Nov-04 9:27
sussPatrick Mc.30-Nov-04 9:27 
GeneralThis is Great Pin
vblackaby21-Sep-04 7:50
vblackaby21-Sep-04 7:50 
GeneralRe: This is Great Pin
TrendyTim22-Sep-04 9:47
TrendyTim22-Sep-04 9:47 
GeneralRe: This is Great Pin
John Gr22-Sep-06 3:13
John Gr22-Sep-06 3:13 
GeneralRe: This is Great Pin
dmbf1b520-Apr-07 1:47
dmbf1b520-Apr-07 1:47 
QuestionRe: This is Great Pin
jmarie18-Jun-07 13:28
jmarie18-Jun-07 13:28 

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.