TreeView with Three-State Checkboxes


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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


Download demo project - 6 Kb


  • Great post but 1 problem

    Posted by SacredFootballLB on 11/20/2009 06:03pm

    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!

  • This example is bad Rated

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

    This example is for VB not for VC

  • P;P;

    Posted by wonjhoncy on 01/12/2005 12:29pm

    Thank you--a Chinese

  • ok

    Posted by cntjlz on 12/30/2004 05:03am


  • thank you

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

    thank you for your code

  • Great idea -- Thanks for sharing

    Posted by Legacy on 01/28/2004 08: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!

  • OK

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

    Originally posted by: Jeff CLark

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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