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

Tri-State TreeView Control

By , 2 Nov 2005
 

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)

About the Author

Ebse
Software Developer (Senior)
Germany Germany
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   
QuestionHow to use this dll in 64 bit mechinesmemberMember 329057315 Sep '10 - 16:18 
Hi,   I tried this dll with my application and application can use this dll in my local mechine(WIndows XP).Then i had deployed the application on 64 bit windows server i got the error message application has stopped working.   Problem signature: Problem Event Name: CLR20r3 ...
AnswerRe: How to use this dll in 64 bit mechinesmemberCody Tang7 Oct '10 - 23:30 
Hello Sajesh,   I have found how to make it work in x64. Open the source code, and make the following changes in the constructor.   public TriStateTreeView() { // This call is required by the Windows.Forms Form Designer. InitializeComponent();   if...
GeneralRe: How to use this dll in 64 bit mechinesmemberMember 223057026 Dec '10 - 12:11 
It Does not work on win7 64x?!?! could you upload a sample on win 64x
GeneralRe: How to use this dll in 64 bit mechinesmemberCody Tang30 Jul '11 - 21:49 
Hello,   If you get the bad image format exception, I think you need to open the source code, remove the reference to SkyVisualBound.dll (follow what I mentioned previously) and build it in "Any CPU". Regards, Cody Tang
GeneralRe: How to use this dll in 64 bit mechinesmemberpman30 Jul '11 - 1:01 
worked, thx!
GeneralImage problemmemberDevon Peterson17 Feb '10 - 12:53 
Hi,   I'm trying to use this control in a VS 2008 project. I'm able to compile it fine and add it to a test form but when I try to run the application I get a BadImageFormatException. Is there a problem with how I am building the dll or something like that? Has anyone else had this...
GeneralRe: Image problemmemberDevon Peterson17 Feb '10 - 13:20 
I've discovered a little more. If I change the build options from Any CPU to x86 then it works. Unfortunately the project I want to integrate this in must build for Any CPU.   Any suggestions?   Devon
GeneralAdded ability to have disabled nodes. You might find this usefull. [modified]memberMuaddubby17 Jun '09 - 2:49 
As noted in a previous message, I found this control extremely useful, so thx a bunch for that.   One thing I was missing, however, was the ability to enable and disable specific tree nodes. Adding an Enabled property to a derived class of the TreeNode would have been too complex because...
GeneralRe: Added ability to have disabled nodes. You might find this usefull.membergiova12 Oct '09 - 22:53 
Thank you for your code Muad'Dubby. Unfortunatly your past failed, look at the last line of your code : if (nodes != null && nodes.Length > seems like something is missing isn't it?
GeneralRe: Added ability to have disabled nodes. You might find this usefull.memberMuaddubby13 Oct '09 - 5:07 
Yes, sorry about that. I'll post the correct code shortly.   Muad'Dubby http://cookingwithcsharp.blogspot.com/
GeneralRe: Added ability to have disabled nodes. You might find this usefull.memberMuaddubby13 Oct '09 - 6:07 
Looks like all the code is available within my comment, but is not being displayed. Probably a maximum string length was hit or something.   In any case, here is the rest of the code, starting from (and including) the last line which appears truncated in my first note). Sorry about this!...
GeneralAbsolutely awesomememberMuaddubby26 May '09 - 3:48 
I was looking for something like this for quite some time. You got my 5!   Muad'Dubby http://cookingwithcsharp.blogspot.com/
Generalcontains node!membernjuniorba7 May '09 - 15:07 
Hi.. how to verify node in tri-state treeview?   example:   if(mytreeview.Contains(mynode)) {   }   I'm not obtaining to make, always it informs that it does not exist, but it exists the node   help please!
GeneralDisable Nodememberkjward20 Apr '09 - 4: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...
GeneralRe: Disable NodememberMuaddubby10 Jun '09 - 8:59 
Hi   I've had the same issue, and got around it by trapping the BeforeCheck event and testing the node that generated the event. If it's the one I want "disabled" I simply cancel the event. I also set the text of the "disabled" node to gray so the user has a visual queue.   Problem...
GeneralRe: Disable NodememberMuaddubby18 Jun '09 - 10:29 
Hi Again   I kept on working on this and came up with a fix that works perfectly for this. See the note I left on June 19 2009, titled "Added ability to have disabled nodes. You might find this usefull. ".   Muad'Dubby http://cookingwithcsharp.blogspot.com/
QuestionTriStateTreeView in Oracle Forms 6imemberchrisvelarde14 Mar '09 - 21:14 
Hi,   The .NET Framework TreeView works fine under Oracle Forms 6i environment and I would like to improve my existing application using the TriStateTreeView instead. Oracle´s Developer 2000 is becoming out of date, but there are still a lot of people/clients that use systems build with...
Generalnice workmemberdrakej11 Oct '08 - 6:39 
The idea of setting both the selected and unselected images for a node to one of the three desired states was not obvious, at least to me when I tried to solve this on my own. Very nice! Thanks.
Generalsuppper control... tx a lot. hat off for the author and code projectmemberkasunth10 Sep '08 - 2:56 
  i'am begnier to the Software engineering field.therefore i would like to refer your site
GeneralAwesome Control!memberkjward6 Sep '08 - 4:57 
thanks ever so much for creating this very cool control. i was using a vs2005 treeview that's populated from an oracle stored procedure resulting in a very complex tree hierarchy. i put your control on my form next to that one as a test, simply changed the name in my data load method to this...
QuestionError using control in VS 2005 (c#)memberNIPI23 Apr '08 - 0:10 
Hello! When i additing the TriStateTreeView control on a form an error: Failed to create component... Could not find any resources appropriate for the specified culture or the neutral culture... What is the problem???
GeneralThis is usable in VB as well!memberjonmach18 Nov '07 - 7:11 
I had a number of problems with the TriStateTreeView class in VB on this site, but would you believe it, it is possible to simply add the .cs file from this project, and add references to the two dll's, and it works perfectly as a valid VB class.   Damn if that isn't outstanding....
GeneralTreeView bugmembermjelten29 Jul '07 - 23:14 
Great Control!   I am using CheckBoxes = true. I notice that when you change Theme in Windows from Windows XP to Windows classic or reverse the TreeView does not Update, you has to restart the application. Anyone know how to fix this or how to work around it?  
Generalcheck box can't be displayedmemberjustanumber29 Jul '07 - 6:21 
I added the TriStateTreeView control on my form, but a check box isn't displayed near the name of the node. I can see that there is some free place where the boxes should appear, but they don't appear, like the images of the boxes can't be loaded. And when i click on them the AfterCheck event is...
GeneralRe: check box can't be displayedmemberCodeForFun20 Sep '07 - 7:49 
Having same problem. Does anyone have a clue?

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

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