65.9K
CodeProject is changing. Read more.
Home

TreeView Control with tri-state Logic

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.57/5 (7 votes)

Oct 2, 2006

CPOL

1 min read

viewsIcon

72645

downloadIcon

556

An override of the default .NET 2.0 TreeView control which supports tri-state logic for its tree nodes

Sample Image - ThreeStateTreeView.png

Introduction

Recently, I needed to add a TreeView control to my project which required a tri-state type checkbox on its nodes. The standard .NET Windows Forms TreeView control supports adding checkboxes to its node, but it does not support tri-state logic on them. After reading a good article (http://www.cherea.de/bitbucket/checktree/checktree.html) about how to do it in MFC, I decided to add this functionality to the Windows Forms TreeView control.

Setting Custom State Images

The .NET TreeView control is simply a wrapper around the standard Tree control which is a part of the Windows common controls. Tree control supports two image lists. One image list is used to display standard icons which you can also set in the .NET TreeView, while the other image list is used to set state images. By giving our own image list to the Tree control, we can choose what we want to display instead of the normal checkboxes provided by TreeView control. The provided ThreeStateTreeView control, in combination with the provided ThreeStateTreeNode does exactly that.

Using the Code

Using the control is the same as using the standard TreeView control. Instead of instantiating the TreeView control, just instantiate the ThreeStateTreeView control.

Issues

  • Setting the state only works after the node has been added to the tree and only after the tree handle has been created (tree has been displayed)
  • By clicking on the new checkbox, you only cycle between checked/unchecked, third state can only be changed programmatically