TreeView with Three-State Checkboxes

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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:

  1. Copy mod3StateTreeViewFunctions.bas and checkboxes.res to your project directory and add them to your project.
  2. Add an ImageList object to the form where your TreeView resides
  3. Initialize ImageList with tri-state checkboxes by calling InitImageList <NameOfImageList>  (see Form_Load sub of the demo form)
  4. Bind the ImageList to your treeview
  5. Populate TreeView with values
  6. 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):

  1. Use the MouseDown event to capture the x and y location of the click.
  2. 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.
  3. <optional> You may want to filter on a specific mouse button (not implemented in demo)
  4. <optional> Use the KeyPress event to capture keyboard ordered state change, I used space to trigger this
  5. If a state change is to occur, call ProcessStateChange passing the node that is about to be changed

Downloads

Download demo project - 6 Kb


Comments

  • Great post but 1 problem

    Posted by SacredFootballLB on 11/20/2009 10:03am

    Really 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!

    Reply
  • This example is bad Rated

    Posted by yonoid on 03/03/2005 02:43pm

    This example is for VB not for VC

    Reply
  • P;P;

    Posted by wonjhoncy on 01/12/2005 04:29am

    Thank you--a Chinese

    Reply
  • ok

    Posted by cntjlz on 12/29/2004 09:03pm

    ok

    Reply
  • thank you

    Posted by feiheng on 11/09/2004 01:06am

    thank you for your code

    Reply
  • Great idea -- Thanks for sharing

    Posted by Legacy on 01/28/2004 12:00am

    Originally 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!

    Reply
  • OK

    Posted by Legacy on 07/25/2001 12:00am

    Originally posted by: Jeff CLark

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date