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

TriStateTreeview in VB.NET

By , 19 Jan 2006
 

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:

  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.

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

About the Author

Carlos J. Quintero
Spain Spain
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOnBeforeExpand & OnBeforeCollapse prevents expand collapse too muchmemberjsytniak2 Mar '09 - 9:55 
Generalcheckbox and afterchange eventmemberiwrschenk3 Dec '08 - 3:18 
AnswerRe: checkbox and afterchange eventmemberjsytniak26 Dec '08 - 10:23 
GeneralProblem with SetTreeNodeState methodmemberOmar Vargas11 Nov '08 - 5:50 
NewsC# Versionmemberjsytniak7 May '08 - 3:37 
Generalnode tooltipmemberkalany16 Apr '08 - 6:21 
GeneralRe: node tooltip - bug with state iconsmemberjsytniak7 May '08 - 3:40 
GeneralC# version with some minor enhancementsmemberalverdal20 Mar '08 - 6:16 
GeneralRe: C# version with some minor enhancementsmemberMember 28343895 Oct '08 - 22:33 
QuestionCheck Boxes not showing up in my projectmembercrazytom25 Jan '08 - 10:03 
GeneralRe: Check Boxes not showing up in my projectmembercrazytom27 Jan '08 - 8:27 
GeneralRe: Check Boxes not showing up in my projectmemberkhendric5 Mar '08 - 3:32 
QuestionProblems with this classmemberjonmach17 Nov '07 - 1:05 
AnswerRe: Problems with this classmemberjonmach17 Nov '07 - 8:18 
AnswerRe: Problems with this classmembermadmax824 Aug '10 - 3:02 
Generalpocket pc projectmemberrabosa20 Jun '07 - 23:58 
GeneralBeforeCheck Issuememberthierry_anthony13 Apr '07 - 7:30 
GeneralPopule windows root [modified]membermpdesign11 Feb '07 - 13:48 
QuestionTreeview state problemmemberAchintya Jha25 Oct '06 - 5:49 
AnswerRe: Treeview state problemmemberpriman_p27 Sep '09 - 21:31 
GeneralLoadOnDemand does not workmemberdevnet24718 Feb '06 - 23:35 
GeneralVery nice however...membervbinfo31 Jan '06 - 4:56 
GeneralRe: Very nice however...memberCarlos J. Quintero31 Jan '06 - 5:17 
GeneralRe: Very nice however...memberdevnet24713 Feb '06 - 12:31 
GeneralRe: Very nice however...memberNewton Burgos5 Aug '06 - 16:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Jan 2006
Article Copyright 2004 by Carlos J. Quintero
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid