TreeView with Three-State Checkboxes

Tuesday Feb 29th 2000 by Mike Korzeniowski

Allows you to write applications that show a third-state of selection for your tree control items

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

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved