5,427,303 members and growing! (14,687 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

TriStateTreeview in VB.NET

By Carlos J. Quintero

A VB.NET implementation of a treeview with 3-state checkboxes using state image list
VB, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 27 Apr 2004
Updated: 19 Jan 2006
Views: 79,642
Bookmarked: 28 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.23 Rating: 3.92 out of 5
1 vote, 8.3%
1
0 votes, 0.0%
2
2 votes, 16.7%
3
3 votes, 25.0%
4
6 votes, 50.0%
5

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



Location: Spain Spain

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
Subject  Author Date 
NewsC# Versionmemberjsytniak4:37 7 May '08  
Generalnode tooltipmemberkalany7:21 16 Apr '08  
GeneralRe: node tooltip - bug with state iconsmemberjsytniak4:40 7 May '08  
GeneralC# version with some minor enhancementsmemberalverdal7:16 20 Mar '08  
QuestionCheck Boxes not showing up in my projectmembercrazytom11:03 25 Jan '08  
GeneralRe: Check Boxes not showing up in my projectmembercrazytom9:27 27 Jan '08  
GeneralRe: Check Boxes not showing up in my projectmemberkhendric4:32 5 Mar '08  
QuestionProblems with this classmemberjonmach2:05 17 Nov '07  
AnswerRe: Problems with this classmemberjonmach9:18 17 Nov '07  
Generalpocket pc projectmemberrabosa0:58 21 Jun '07  
GeneralBeforeCheck Issuememberthierry_anthony8:30 13 Apr '07  
GeneralPopule windows root [modified]membermpdesign14:48 11 Feb '07  
QuestionTreeview state problemmemberAchintya Jha6:49 25 Oct '06  
GeneralLoadOnDemand does not workmemberdevnet2470:35 19 Feb '06  
GeneralVery nice however...membervbinfo5:56 31 Jan '06  
GeneralRe: Very nice however...memberCarlos J. Quintero6:17 31 Jan '06  
GeneralRe: Very nice however...memberdevnet24713:31 13 Feb '06  
GeneralRe: Very nice however...memberNewton Burgos17:54 5 Aug '06  
GeneralGreat Jobmembercomputerguru9238217:54 24 Jan '06  
GeneralMissing exe/dllmemberJonas Beckeman12:17 18 Jan '06  
GeneralRe: Missing exe/dllmemberCarlos J. Quintero23:24 18 Jan '06  
GeneralGreat !!memberplasticgoat6:04 18 Aug '05  
Generalhow to do it in asp.net?membersteven_wong17:42 23 May '05  
GeneralExcellent - but needed C++!memberskonopa10:34 28 Feb '05  
GeneralRe: Excellent - but needed C++!memberskonopa13:42 1 Mar '05  

General General    News News    Question Question