Click here to Skip to main content
12,950,099 members (40,003 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 4 Jun 2006

How to handle custom node state images in a TreeView (e.g. tristate checkboxes)

, 4 Jun 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Use custom state images for your nodes within minutes.

Sample Image - customstatetreeview.jpg


Caused by the new features of .NET 2.0 and VS 2005 I started to write the Enhanced BrowseForFolder styled TreeView (TreeViewStrategy) code from the ground up new. As I came along the new TreeView features, I figured out that it's now possible to use state images in an easy way. So my first take was to implement tristate checkboxes, a useful feature in a folder browser. I realized that, with a few lines of code, state images can be used in many scenarios, that's the reason why you're reading these lines. Hope it's useful for your development as well.


CustomStateTreeView inherits from System.Windows.Forms.TreeView and expects its nodes to implement the IStateTreeNode interface. The following events/methods are overridden to provide the custom state check logic:

  • OnHandleCreated - Used to create the StateImageList.
  • OnKeyDown - Hit testing.
  • OnMouseDown - Hit testing.

To override, these methods became necessary because:

  • out of the box, the control only handles node states if CheckBoxes are enabled.
  • the boolean check flag of a System.Windows.Forms.TreeNode just knows two states.

To implement the desired behaviour, I wrote custom code to handle the node state. The tree view now delegates the state change to the node. The node itself is responsible to change its state accurate to its current context and data.

KeyDown and MouseDown now determine the hit test result to check if the user changed the check state of a certain node. If this happens, the custom Check method is called.

System.Windows.Forms.TreeViewHitTestInfo info = 
                             this.HitTest(e.X, e.Y);
if (info.Node != null && info.Location.ToString() == "StateImage")

The OnCustomCheck method preserves the BeforeCheck and AfterCheck event logic. So it's still possible to cancel a Check action.

protected virtual void OnCustomCheck(System.Windows.Forms.TreeNode node, 
                  System.Windows.Forms.TreeViewAction action)
      System.Windows.Forms.TreeViewCancelEventArgs e = new 
         System.Windows.Forms.TreeViewCancelEventArgs(node, false, action);
      if (e.Cancel) return;
      if (node is IStateTreeNode)
      OnAfterCheck(new System.Windows.Forms.TreeViewEventArgs(node, action));

E.g.: for the tri-state tree node, I used the following interface implementation:

public virtual System.Windows.Forms.CheckState CheckState
      return _checkState;
       _checkState = value;
       StateImageIndex = (int)value;

void IStateTreeNode.UpdateState(System.Windows.Forms.TreeViewCancelEventArgs e)
    switch (CheckState)
        case System.Windows.Forms.CheckState.Checked:
            this.CheckState = System.Windows.Forms.CheckState.Indeterminate;
        case System.Windows.Forms.CheckState.Indeterminate:
            CheckState = System.Windows.Forms.CheckState.Unchecked;
        case System.Windows.Forms.CheckState.Unchecked:
            CheckState = System.Windows.Forms.CheckState.Checked;


To use state images in your project, add CustomStateTreeView.cs and derive your tree view from CustomStateTreeView. Override CreateStateImages() to fill your state images into the StateImageList. On the other side, implement IStateTreeNode to your TreeNode classes, and switch StateImageIndex there to show the image you added to visualize the desired state.


Tri-state nodes are nice, but bear in mind that features like load on demand and complex recursive node checking aren't part of this article. This one just lets you use custom state images with a few lines of code.



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


About the Author

Chris Richner
Software Developer (Senior) Zeit AG
Switzerland Switzerland
My interest is in the future because I am going to spend the rest of my life there. (Charles Kettering)


  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP

  • 1999 - 2001 coding Centura against Sql Database (SqlBase,MSSQL,Oracle)

  • 2002 - 2004 C# Windows Forms

  • 2005 - 2006 C# ASP.NET, Windows Forms

  • 2006 - 2009 C#, WCF, WF, WPF

  • 2010 - 2012 C#, Dynamics CRM, Sharepoint, Silverlight

  • 2013 - 2013 C#, WCF DS (OData), WF, WPF

  • 2014 - now C#, Azure PaaS, Identity, OWIN, OData, Web Api


  • family & friends

  • chilaxing ,)

  • coding

You may also be interested in...

Comments and Discussions

GeneralBypassing NET StateImageList size restriction Pin
OrlandoCurioso6-Jun-06 2:01
memberOrlandoCurioso6-Jun-06 2:01 
GeneralRe: Bypassing NET StateImageList size restriction Pin
Chris Richner6-Jun-06 2:51
memberChris Richner6-Jun-06 2:51 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 5 Jun 2006
Article Copyright 2006 by Chris Richner
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid