Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML

TreeView Control with tri-state Logic

Rate me:
Please Sign up or sign in to vote.
1.30/5 (10 votes)
12 Jan 2010CPOL1 min read 70.6K   554   18   25
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Croatia Croatia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General2 suggestion Pin
Mr.PoorEnglish3-Mar-10 2:25
Mr.PoorEnglish3-Mar-10 2:25 
you can solve Issue 2:
"by clicking on the new checkbox you only cycle between checked/unchecked, third state can only be changed programmaticaly"


with another OnAfterCheck:
C#
protected override void OnAfterCheck(TreeViewEventArgs e) {
   base.OnAfterCheck(e);
   ThreeStateTreeNode node = e.Node as ThreeStateTreeNode;
   if(node == null) return;
   node.ThreeStateChecked = (ThreeState)(((int)(node.ThreeStateChecked) + 1) % 3);
}

please add a second level to the treeview of the demo:
C#
public partial class MainForm : Form {
   public MainForm() {
      InitializeComponent();
      foreach(var item in System.IO.Directory.GetDirectories("c:\\")) {
         var nd = new TreeViewThreeState.ThreeStateTreeNode(item);
         _threeState.Nodes.Add(nd);
         try {
            foreach(var item2 in System.IO.Directory.GetDirectories(nd.FullPath)) {
              nd.Nodes.Add(new TreeViewThreeState.ThreeStateTreeNode(item2));
            }
         } catch(UnauthorizedAccessException) { }
      }
   }
}

GeneralRe: 2 suggestion Pin
Dalibor Carapic3-Mar-10 2:42
Dalibor Carapic3-Mar-10 2:42 
JokeRe: 2 suggestion Pin
Mr.PoorEnglish3-Mar-10 3:39
Mr.PoorEnglish3-Mar-10 3:39 
Generalstrange issue Pin
Mr.PoorEnglish3-Mar-10 1:02
Mr.PoorEnglish3-Mar-10 1:02 
QuestionHow this ended... Pin
Paw Jershauge12-Jan-10 1:32
Paw Jershauge12-Jan-10 1:32 
AnswerRe: How this ended... Pin
Dalibor Carapic12-Jan-10 5:17
Dalibor Carapic12-Jan-10 5:17 
GeneralRe: How this ended... Pin
Paw Jershauge12-Jan-10 5:31
Paw Jershauge12-Jan-10 5:31 
GeneralRe: How this ended... Pin
Paw Jershauge12-Jan-10 5:33
Paw Jershauge12-Jan-10 5:33 
GeneralRe: How this ended... Pin
Dalibor Carapic12-Jan-10 8:43
Dalibor Carapic12-Jan-10 8:43 
GeneralRe: How this ended... Pin
Paw Jershauge12-Jan-10 11:35
Paw Jershauge12-Jan-10 11:35 
AnswerRe: How this ended... Pin
Mr.PoorEnglish3-Mar-10 0:50
Mr.PoorEnglish3-Mar-10 0:50 
GeneralNo Works Pin
luisxvarg11-Jan-10 21:31
luisxvarg11-Jan-10 21:31 
GeneralRe: No Works Pin
Dalibor Carapic12-Jan-10 1:21
Dalibor Carapic12-Jan-10 1:21 
Questiona question from Zin Mar Pin
Sean Ewington27-May-07 16:13
staffSean Ewington27-May-07 16:13 
AnswerRe: a question from Zin Mar Pin
Dalibor Carapic27-May-07 20:16
Dalibor Carapic27-May-07 20:16 
QuestionHow? Pin
lessms20-May-07 14:43
lessms20-May-07 14:43 
AnswerRe: How? Pin
lessms20-May-07 14:56
lessms20-May-07 14:56 
AnswerRe: How? Pin
dc100020-May-07 20:46
dc100020-May-07 20:46 
GeneralRe: How? Pin
lessms21-May-07 4:41
lessms21-May-07 4:41 
GeneralRe: How? Pin
dc100021-May-07 4:55
dc100021-May-07 4:55 
GeneralRe: How? Pin
lessms21-May-07 5:19
lessms21-May-07 5:19 
GeneralRe: How? Pin
Dalibor Carapic21-May-07 20:31
Dalibor Carapic21-May-07 20:31 
GeneralMaking the tri state tree theme aware Pin
situssoft23-Mar-07 3:49
situssoft23-Mar-07 3:49 
Generalproblem adding this project to a ASP.NET website Pin
mohsen nowruzi6-Nov-06 4:33
mohsen nowruzi6-Nov-06 4:33 
GeneralRe: problem adding this project to a ASP.NET website Pin
Chris Richner15-Mar-07 2:37
Chris Richner15-Mar-07 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.