Click here to Skip to main content
11,412,833 members (74,177 online)
Click here to Skip to main content

Tri-State TreeView Control

, 2 Nov 2005 CPL
Rate this:
Please Sign up or sign in to vote.
A TreeView control with tri-state checkboxes.

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, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)

Share

About the Author

Ebse
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionThis Post is very helpul for me Pin
ashishgupta1212 at 5-Mar-13 9:46
memberashishgupta12125-Mar-13 9:46 
QuestionGracias Pin
jc221187 at 1-Mar-13 16:08
memberjc2211871-Mar-13 16:08 
QuestionNot Displaying in Form Pin
podal at 14-Dec-12 17:37
memberpodal14-Dec-12 17:37 
QuestionThank you! Pin
krillgar at 2-Mar-12 12:32
memberkrillgar2-Mar-12 12:32 
Questionmuy bueno Pin
Member 7722713 at 18-Nov-11 11:50
memberMember 772271318-Nov-11 11:50 
BugRight To Left Pin
nagham_4ng at 1-Aug-11 0:10
membernagham_4ng1-Aug-11 0:10 
GeneralRe: Right To Left Pin
Ebse at 17-Aug-11 6:20
memberEbse17-Aug-11 6:20 
GeneralMy vote of 5 Pin
vstetsko at 7-Mar-11 10:13
membervstetsko7-Mar-11 10:13 
QuestionUnusual Behavior Pin
Phantom208 at 1-Feb-11 7:35
memberPhantom2081-Feb-11 7:35 
AnswerRe: Unusual Behavior Pin
Phantom208 at 2-Feb-11 6:55
memberPhantom2082-Feb-11 6:55 
QuestionHow to use this dll in 64 bit mechines Pin
Member 3290573 at 15-Sep-10 17:18
memberMember 329057315-Sep-10 17:18 
AnswerRe: How to use this dll in 64 bit mechines Pin
Cody Tang at 8-Oct-10 0:30
memberCody Tang8-Oct-10 0:30 
GeneralRe: How to use this dll in 64 bit mechines Pin
Member 2230570 at 26-Dec-10 13:11
memberMember 223057026-Dec-10 13:11 
GeneralRe: How to use this dll in 64 bit mechines Pin
Cody Tang at 30-Jul-11 22:49
memberCody Tang30-Jul-11 22:49 
GeneralRe: How to use this dll in 64 bit mechines Pin
pman at 30-Jul-11 2:01
memberpman30-Jul-11 2:01 
GeneralImage problem Pin
Devon Peterson at 17-Feb-10 13:53
memberDevon Peterson17-Feb-10 13:53 
GeneralRe: Image problem Pin
Devon Peterson at 17-Feb-10 14:20
memberDevon Peterson17-Feb-10 14:20 
GeneralAdded ability to have disabled nodes. You might find this usefull. [modified] Pin
Muaddubby at 17-Jun-09 3:49
memberMuaddubby17-Jun-09 3:49 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
giova at 12-Oct-09 23:53
membergiova12-Oct-09 23:53 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
Muaddubby at 13-Oct-09 6:07
memberMuaddubby13-Oct-09 6:07 
GeneralRe: Added ability to have disabled nodes. You might find this usefull. Pin
Muaddubby at 13-Oct-09 7:07
memberMuaddubby13-Oct-09 7:07 
GeneralAbsolutely awesome Pin
Muaddubby at 26-May-09 4:48
memberMuaddubby26-May-09 4:48 
Generalcontains node! Pin
njuniorba at 7-May-09 16:07
membernjuniorba7-May-09 16:07 
GeneralDisable Node Pin
kjward at 20-Apr-09 5:53
memberkjward20-Apr-09 5:53 
i've been using this control very successfully for a year or more...thanks ever so much for all your great work!

i'd like to be able to disable certain nodes based on some conditions; can this be done?

my current workaround is to remove the node altogether, but that has its drawbacks. it really would be better to show the node but have it disabled.

thanks

kjward

GeneralRe: Disable Node Pin
Muaddubby at 10-Jun-09 9:59
memberMuaddubby10-Jun-09 9:59 
GeneralRe: Disable Node Pin
Muaddubby at 18-Jun-09 11:29
memberMuaddubby18-Jun-09 11:29 
QuestionTriStateTreeView in Oracle Forms 6i Pin
chrisvelarde at 14-Mar-09 22:14
memberchrisvelarde14-Mar-09 22:14 
Generalnice work Pin
drakej1 at 1-Oct-08 7:39
memberdrakej11-Oct-08 7:39 
Generalsuppper control... tx a lot. hat off for the author and code project Pin
kasunth at 10-Sep-08 3:56
memberkasunth10-Sep-08 3:56 
GeneralAwesome Control! Pin
kjward at 6-Sep-08 5:57
memberkjward6-Sep-08 5:57 
QuestionError using control in VS 2005 (c#) Pin
NIPI at 23-Apr-08 1:10
memberNIPI23-Apr-08 1:10 
GeneralThis is usable in VB as well! Pin
jonmach at 18-Nov-07 8:11
memberjonmach18-Nov-07 8:11 
GeneralTreeView bug Pin
mjelten at 30-Jul-07 0:14
membermjelten30-Jul-07 0:14 
Generalcheck box can't be displayed Pin
justanumber at 29-Jul-07 7:21
memberjustanumber29-Jul-07 7:21 
GeneralRe: check box can't be displayed Pin
CodeForFun at 20-Sep-07 8:49
memberCodeForFun20-Sep-07 8:49 
GeneralRe: check box can't be displayed Pin
zz_lsl at 20-Dec-10 4:40
memberzz_lsl20-Dec-10 4:40 
GeneralOnBeforeCheck [modified] Pin
mjelten at 19-Jul-07 23:23
membermjelten19-Jul-07 23:23 
QuestionReset all to unchecked? Pin
Jim Baldwin at 24-Jun-07 20:30
memberJim Baldwin24-Jun-07 20:30 
QuestionHow to add multiple images on same tree node? Pin
sushantya at 6-Mar-07 20:39
membersushantya6-Mar-07 20:39 
GeneralTri-State TreeView in Pocket Project Pin
Silas Pereira at 27-Feb-07 2:08
memberSilas Pereira27-Feb-07 2:08 
QuestionHow do I get what is checked? Pin
JoppeG at 26-Feb-07 22:33
memberJoppeG26-Feb-07 22:33 
AnswerArrayList Bug? Pin
JoppeG at 28-Feb-07 1:55
memberJoppeG28-Feb-07 1:55 
GeneralSolved problem Pin
JoppeG at 28-Feb-07 5:07
memberJoppeG28-Feb-07 5:07 
GeneralRe: Solved problem Pin
jonmach at 18-Nov-07 8:38
memberjonmach18-Nov-07 8:38 
QuestionRight-To-Left Problem Pin
morali2k at 2-Sep-06 3:58
membermorali2k2-Sep-06 3:58 
GeneralSimplified tree-state TreeView Pin
Laurent Muller at 22-Aug-06 5:35
memberLaurent Muller22-Aug-06 5:35 
GeneralRe: Simplified tree-state TreeView Pin
UncleRedz at 1-Nov-06 5:18
memberUncleRedz1-Nov-06 5:18 
GeneralRe: Simplified tree-state TreeView Pin
barclay at 19-Dec-06 12:44
memberbarclay19-Dec-06 12:44 
GeneralRe: Simplified tree-state TreeView Pin
hamlet_ at 13-Feb-07 1:38
memberhamlet_13-Feb-07 1:38 
GeneralRe: Simplified tree-state TreeView Pin
Martin Welker at 4-Jun-07 13:17
memberMartin Welker4-Jun-07 13:17 

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.

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