Click here to Skip to main content
Licence 
First Posted 28 Mar 2004
Views 212,198
Downloads 2,077
Bookmarked 66 times

Tri-State TreeView Control

By Ebse | 2 Nov 2005
A TreeView control with tri-state checkboxes.
1 vote, 3.7%
1

2
2 votes, 7.4%
3
4 votes, 14.8%
4
20 votes, 74.1%
5
4.43/5 - 27 votes
1 removed
μ 4.35, σa 1.63 [?]

Introduction

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.

Background

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) == 
         TriStateTreeView.CheckState.Checked)
    DoFoo(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.

Limitations

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.

Remarks

The control makes use of the Skybound.VisualStyle assembly from www.skybound.ca. 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.

History

  • 26 March 2004 - First version.
  • 28 October 2005 - Added support for visual styles and Before/AfterCheck.

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

Ebse

Software Developer (Senior)

Germany Germany

Member


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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionmuy bueno PinmemberMember 772271311:50 18 Nov '11  
BugRight To Left Pinmembernagham_4ng0:10 1 Aug '11  
GeneralRe: Right To Left PinmemberEbse6:20 17 Aug '11  
GeneralMy vote of 5 Pinmembervstetsko10:13 7 Mar '11  
QuestionUnusual Behavior PinmemberPhantom2087:35 1 Feb '11  
AnswerRe: Unusual Behavior PinmemberPhantom2086:55 2 Feb '11  
QuestionHow to use this dll in 64 bit mechines PinmemberMember 329057317:18 15 Sep '10  
AnswerRe: How to use this dll in 64 bit mechines PinmemberCody Tang0:30 8 Oct '10  
GeneralRe: How to use this dll in 64 bit mechines PinmemberMember 223057013:11 26 Dec '10  
GeneralRe: How to use this dll in 64 bit mechines PinmemberCody Tang22:49 30 Jul '11  
GeneralRe: How to use this dll in 64 bit mechines Pinmemberpman2:01 30 Jul '11  
GeneralImage problem PinmemberDevon Peterson13:53 17 Feb '10  
GeneralRe: Image problem PinmemberDevon Peterson14:20 17 Feb '10  
GeneralAdded ability to have disabled nodes. You might find this usefull. [modified] PinmemberMuaddubby3:49 17 Jun '09  
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pinmembergiova23:53 12 Oct '09  
GeneralRe: Added ability to have disabled nodes. You might find this usefull. PinmemberMuaddubby6:07 13 Oct '09  
GeneralRe: Added ability to have disabled nodes. You might find this usefull. PinmemberMuaddubby7:07 13 Oct '09  
GeneralAbsolutely awesome PinmemberMuaddubby4:48 26 May '09  
Generalcontains node! Pinmembernjuniorba16:07 7 May '09  
GeneralDisable Node Pinmemberkjward5:53 20 Apr '09  
GeneralRe: Disable Node PinmemberMuaddubby9:59 10 Jun '09  
GeneralRe: Disable Node PinmemberMuaddubby11:29 18 Jun '09  
QuestionTriStateTreeView in Oracle Forms 6i Pinmemberchrisvelarde22:14 14 Mar '09  
Generalnice work Pinmemberdrakej17:39 1 Oct '08  
Generalsuppper control... tx a lot. hat off for the author and code project Pinmemberkasunth3:56 10 Sep '08  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 2 Nov 2005
Article Copyright 2004 by Ebse
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid