Click here to Skip to main content
15,400,433 members
Articles / Desktop Programming / Windows Forms
Posted 10 Jul 2006


529 bookmarked

Advanced TreeView for .NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (177 votes)
10 Jul 20064 min read
The TreeViewAdv control is designed to replace the standard .NET TreeView. It can do the same things, plus a number of advanced features like multi-selection or multi-column view.

TreeViewAdv in Multi-Column mode


Working on several different projects, I was needed to display and edit hierarchical data. Of course, the first thing you will do is to use the standard .NET TreeView control. It works pretty well if you only need basic features. But learning this control to do something more complex is not an easy job. I could not find an alternative TreeView control which is free and fully meets my needs, so finally I decided to write my own.

The architecture of this control comes mainly from the Java Swing component, with some modifications. These are the key features of the TreeViewAdv control:

  • Model-View architecture - Will be covered in a separate section of this article.
  • Multiselection - Maybe the first limitation which you will find in the standard TreeView is that it’s not possible to select more then one node.
  • Unlimited number of controls for each node - You can display three icons + a CheckBox + two Labels.
  • Multicolumns - You can split the TreeView into several columns.
  • Load on Demand - Lazy load of child nodes.
  • Drag & Drop highlighting - Dynamically highlight the drop position.
  • 100% pure .NET code - No WinAPI is used in this control.

The following screenshots illustrate the TreeViewAdv features:

Drag&Drop highlighting


Using ComboBox to edit node

Model-View Architecture

I really like the Model-View pattern, and decided to use it in this control. The main idea of this pattern is to split the model (business object) from its visualization (control). If the model changes, it notifies the view by firing corresponding events. The view asks the model for details, if needed, and displays the changes. The model is described by ITreeModelInterface:

public interface ITreeModel
    IEnumerable GetChildren(TreePath treePath);
    bool IsLeaf(TreePath treePath);

    event EventHandler<TreeModelEventArgs> NodesChanged; 
    event EventHandler<TreeModelEventArgs> NodesInserted;
    event EventHandler<TreeModelEventArgs> NodesRemoved; 
    event EventHandler<TreePathEventArgs> StructureChanged;

It’s very simple, and you need to implement only two methods. GetChildren should return the list of child nodes of the specified parent (empty for root nodes). IsLeaf method tells TreeView whether it should try to read child nodes of the specified parent. If you wish TreeView to dynamically track model changes, you need to use one of several events of the ITreeModel interface. The most common is the StructureChanged event, which cause the TreeView to fully refresh the specified node (or empty, for the whole model). For example, see the default implementation of the ITreeModel interface – the TreeModel class.

To specify the exact node in the model, TreePath class is used. It stores the path from the root to the node, in the FullPath property.

public class TreePath
    public object[] FullPath{ get; }
    public object LastNode{ get; }
    public object FirstNode{ get; }

Using TreeView

In the source code, you can find two examples of how to use TreeViewAdv. The simplest way is to use TreeModel. All you need is to populate it with data and display it in the view:

_model = new TreeModel();
_model.Nodes.Add(new Node("Root"));
_tree.Model = _model;

The Node class, which is used in TreeModel, contains only the ‘Text’ and ‘IsChecked’ properties. If you need additional properties, you can create an ancestor of the Node class and use it in TreeModel.

But to use the full power of the TreeViewAdv, you should create your own realization of the ITreeModel interface. See the folder browser presented in the source code, for an example.

Customizing TreeView

There are a number of properties which help to customize the look and behavior of the TreeView. The main ones are:

  • Model - Assign your model to this property to display it.
  • NodeControls - The collection of controls which will be used to visualize the model. You should provide at least one NodeControl in order to see the model.
  • LoadOnDemand - Read all child nodes at start-up or when the parent node expands.
  • SelectionMode - Single (no multi-selection), Multi, MultiSameParent (children of only one node can be selected).
  • UseColumns - Display data in columns or not.
  • Columns - The collection of columns. For each column, you can specify its header, width and alignment.


The standard TreeView can display only one icon, CheckBox, and Label for each node. In TreeViewAdv, you can use any number of NodeControl. All controls must inherit from the ‘NodeControl’ abstract class. Inherited classes should contain the code to draw the control and the code to respond on user actions – mouse and keyboard events.


This is the class diagram of all NodeControls provided by the library:

Class diagram

The BindableControl class provides a ‘DataPropertyName’ which is used in the control to read and write data to the node. All that you need is to specify the name of the property of your class.

Terms and Conditions

The TreeViewAdv control is provided as free software with open source code. You can use it in your applications if the original copyright is kept.

The latest version of TreeViewAdv is always available here. Please feel free to add your comments and suggestions in the forum there.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Andrey Gliznetsov
Software Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

QuestionSpecify Node root of the TreeView Pin
medericmartin30-Jul-10 2:20
Membermedericmartin30-Jul-10 2:20 
QuestionHow to set only some nodes to readonly? Pin
tyarseneau16-Jul-10 11:07
Membertyarseneau16-Jul-10 11:07 
AnswerRe: How to set only some nodes to readonly? [modified] Pin
tyarseneau20-Jul-10 10:07
Membertyarseneau20-Jul-10 10:07 
GeneralLicense Pin
Member 270074015-Jul-10 18:07
MemberMember 270074015-Jul-10 18:07 
AnswerRe: License Pin
lastunicorn12-Oct-10 13:35
Memberlastunicorn12-Oct-10 13:35 
Generalprogressbar in treeviewadv Pin
trancefreak15-Jun-10 7:17
Membertrancefreak15-Jun-10 7:17 
QuestionHow to automatically select all childs in a node if the parent checkbox is selected Pin
SeaWater2-Jun-10 5:45
MemberSeaWater2-Jun-10 5:45 
AnswerRe: How to automatically select all childs in a node if the parent checkbox is selected Pin
SeaWater3-Jun-10 4:25
MemberSeaWater3-Jun-10 4:25 
To answer my own question, I got it to work. Big Grin | :-D Keep in mind the code below is only if you have one level deep of childs. Any deeper than one level is not supported with the code below. Maybe another contributing developer can post code with more than one level of deepness.

First setup an event to handle the NodeMouseClick. This can be done when you load the form or whenever you are populating your tree:

trvToDoList.NodeMouseClick += new EventHandler<TreeNodeAdvMouseEventArgs>(trvToDoList_NodeMouseClick);

The next step is to handle the event. (Note: this event will fire even if they dont click on a checkbox. If they click on the + and - signs, the event will still fire but should not affect you):

void trvToDoList_NodeMouseClick(object sender, TreeNodeAdvMouseEventArgs e)


    if ((e.Node.Tag as yourNodeModel).nYourCheckBox == true &&
        e.Node.Children.Count != 0)

        // Select All
        for (int x = 0; x < e.Node.Children.Count; x++)
            (e.Node.Children[x].Tag as yourNodeModel).nYourCheckBox = true;

    else if ((e.Node.Tag as yourNodeModel).nYourCheckBox == false &&
             e.Node.Children.Count != 0)

        // Unselect All
        for (int x = 0; x < e.Node.Children.Count; x++)
            (e.Node.Children[x].Tag as yourNodeModel).nYourCheckBox = false;
    else if ((e.Node.Tag as yourNodeModel).nYourCheckBox == true &&
             e.Node.Children.Count == 0)
        // if we are selecting a child, then check all childs, if they
        // are all checked, then check parent box
        for (int x = 0; x < e.Node.Parent.Children.Count; x++)
            if ((e.Node.Parent.Children[x].Tag as yourNodeModel).nYourCheckBox == false)
                return; // if we find an unchecked node, exit function
        // if we got this far, set parent also as checked
        (e.Node.Parent.Tag as yourNodeModel).nYourCheckBox = true;
    else if ((e.Node.Tag as yourNodeModel).nYourCheckBox == false &&
             e.Node.Children.Count == 0)
        // if we are unselecting a child, then remove parent checkbox
        (e.Node.Parent.Tag as yourNodeModel).nYourCheckBox = false;



    // This is required...we need to wait 1 second before next click
    // if not checkboxes will get out of sync if they "fast click" on and off
    // maybe someone has a better way were this is not needed


QuestionHow to add items into this Advanced TreeView having columns Pin
tamour ahmad28-Apr-10 3:33
Membertamour ahmad28-Apr-10 3:33 
AnswerRe: How to add items into this Advanced TreeView having columns Pin
DAvenLo1-May-10 22:20
MemberDAvenLo1-May-10 22:20 
GeneralQuestion Pin
thready19-Apr-10 18:49
Memberthready19-Apr-10 18:49 
GeneralRe: Question Pin
DAvenLo26-Apr-10 2:05
MemberDAvenLo26-Apr-10 2:05 
AnswerRe: Question [modified] Pin
DAvenLo1-May-10 21:54
MemberDAvenLo1-May-10 21:54 
GeneralRe: Question Pin
thready2-May-10 3:38
Memberthready2-May-10 3:38 
GeneralRe: Question [modified] Pin
DAvenLo3-May-10 20:17
MemberDAvenLo3-May-10 20:17 
QuestionHow can i change BackgroundBrush in other columns than first? Pin
marcin.swi6-Apr-10 21:22
Membermarcin.swi6-Apr-10 21:22 
QuestionIs it possible to display bitmaps in columns? Pin
usg4-Mar-10 11:56
Memberusg4-Mar-10 11:56 
GeneralError in TreeViewAdv Pin
blacksaifer24-Feb-10 15:35
Memberblacksaifer24-Feb-10 15:35 
QuestionCheckboxes are not in the correct position when moving columns Pin
Member 167849515-Jan-10 7:00
MemberMember 167849515-Jan-10 7:00 
GeneralIsVisibleValueNeeded set to true but also want the Parents to be visible as well Pin
turinreza14-Jan-10 9:33
Memberturinreza14-Jan-10 9:33 
GeneralRe: IsVisibleValueNeeded set to true but also want the Parents to be visible as well Pin
turinreza14-Jan-10 10:13
Memberturinreza14-Jan-10 10:13 
Question[My vote of 1] How do I enable tri-state checkboxes? Pin
Mike Martell18-Dec-09 9:31
MemberMike Martell18-Dec-09 9:31 
Generalnodecontrol textinput box mapped to int value of datarow Pin
turinreza16-Dec-09 2:16
Memberturinreza16-Dec-09 2:16 
Questioncheckbox cant check iwhen using Columns? Pin
turinreza14-Dec-09 12:42
Memberturinreza14-Dec-09 12:42 
AnswerRe: checkbox cant check iwhen using Columns? Pin
keyboarder29-Aug-13 11:00
professionalkeyboarder29-Aug-13 11:00 

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.