TreeView with Three-State Checkboxes
Over the last few months, I have been involved with a cople of projects that required the use of checkboxes in a TreeView control. One of the frustrations with the standard TreeView is that you can't show a partial selection to indicate that some of the children are selected. A partial selection in a complex tree allows the user to easily determine where the selections are, even if the tree is not completely expanded. A similar idea is used in manu backup programs to allow the user to select the files to be backed up.
With the current project requiring such a TreeView written in VB, I have come up with a simple solution for this problem. The attached code displays a bitmap next to the node label. Depending on the selections in the parent and children nodes, the bitmap displayed will show a checked, unchecked or partially checked box. There are also two functions to process a state change and update the nedes above and below the node which changed state.
To add this sample to your project do the following:
- Copy mod3StateTreeViewFunctions.bas and checkboxes.res to your project directory and add them to your project.
- Add an ImageList object to the form where your TreeView resides
- Initialize ImageList with tri-state checkboxes by calling InitImageList <NameOfImageList> (see Form_Load sub of the demo form)
- Bind the ImageList to your treeview
- Populate TreeView with values
- nbsp; <optional> Set checkboxes in the TreeView, updating the parents and children
To capture a state change event (see demo form for the sample code):
- Use the MouseDown event to capture the x and y location of the click.
- Use the Click event to check if the click is near the checkbox bitmap. This is tricky as the offset of nodeTest in the demo may need to be adjusted depending on the ScaleMode you use. I used Twips, the default selection.
- <optional> You may want to filter on a specific mouse button (not implemented in demo)
- <optional> Use the KeyPress event to capture keyboard ordered state change, I used space to trigger this
- If a state change is to occur, call ProcessStateChange passing the node that is about to be changed
Comments
Great post but 1 problem
Posted by SacredFootballLB on 11/20/2009 10:03amReally like your post. Been looking for hours for a way to do something like this. I cannot seem to get the 3rd state to work for some reason. The boxes only state checked or unchecked; never grayed out checked. any ideas would be immensely appreciated!
ReplyThis example is bad Rated
Posted by yonoid on 03/03/2005 02:43pmThis example is for VB not for VC
ReplyP;P;
Posted by wonjhoncy on 01/12/2005 04:29amThank you--a Chinese
Replyok
Posted by cntjlz on 12/29/2004 09:03pmthank you
Posted by feiheng on 11/09/2004 01:06amthank you for your code
ReplyGreat idea -- Thanks for sharing
Posted by Legacy on 01/28/2004 12:00amOriginally posted by: Cliff Zimmermam
Brad Martinez (http://www.mvps.org/btmtz/treeview/) published a set of code that would let you change the checkbox images by using the API to change the underlying treeview properties. With those routines you could set the node image and also change the appearance of the checkbox. But if all you need is the checkboxes and you don't care about the images, Mike's method is great. I cut/pasted the code into my project, made some superficial formatting changes, and it worked as advertised. Thanks for taking the trouble to share this, Mike!
ReplyOK
Posted by Legacy on 07/25/2001 12:00amOriginally posted by: Jeff CLark