Click here to Skip to main content
15,881,872 members
Articles / Desktop Programming / Windows Forms
Article

Advanced TreeView for .NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (178 votes)
10 Jul 20064 min read 1.2M   33.7K   532   304
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

Introduction

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

Multiselection

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:

C#
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.

C#
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:

C#
_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.

NodeControls

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.

NodeControl

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.

License

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


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

Comments and Discussions

 
GeneralRe: Improved AutoSizeColumn function Pin
Summersyu12-Jul-11 7:02
Summersyu12-Jul-11 7:02 
GeneralRe: Improved AutoSizeColumn function Pin
ub3rst4r12-Jul-11 14:35
ub3rst4r12-Jul-11 14:35 
QuestionIs it possible to make tree column frozen? Pin
Marcin Smolka29-Oct-09 4:07
Marcin Smolka29-Oct-09 4:07 
GeneralMore Documentation - Cannot get text of node to display Pin
Killme00700713-Oct-09 18:41
Killme00700713-Oct-09 18:41 
AnswerRe: More Documentation - Cannot get text of node to display [modified] Pin
MasonMcCuskey28-Feb-10 14:40
MasonMcCuskey28-Feb-10 14:40 
GeneralPerhaps a content bug in the TreeView control (SMALL) Pin
LimitedAtonement5-Oct-09 6:36
LimitedAtonement5-Oct-09 6:36 
QuestionHow to disable the default tooltip of expandable node in asp.net 2.0 Pin
hetak\lkumar30-Sep-09 2:03
hetak\lkumar30-Sep-09 2:03 
GeneralPerformance Problem with 10.000 or More Nodes Pin
Michael Biener28-Sep-09 2:47
Michael Biener28-Sep-09 2:47 
AnswerRe: Performance Problem with 10.000 or More Nodes Pin
yetibrain30-Sep-09 0:54
yetibrain30-Sep-09 0:54 
GeneralAdvTreeView Multi Line Header Pin
areB19-Sep-09 4:17
areB19-Sep-09 4:17 
GeneralTreeView used in DBSourceTools Pin
NathanRozentals17-Sep-09 3:23
NathanRozentals17-Sep-09 3:23 
QuestionCannot set model for inserted root Node. Pin
YevgenBorodkin20-Aug-09 6:19
YevgenBorodkin20-Aug-09 6:19 
AnswerRe: Cannot set model for inserted root Node. Pin
dan_bock7-Oct-09 5:34
dan_bock7-Oct-09 5:34 
Questioncannot run this project [modified] Pin
coolkid171215-Aug-09 19:05
coolkid171215-Aug-09 19:05 
AnswerRe: cannot run this project Pin
coolkid171216-Aug-09 1:01
coolkid171216-Aug-09 1:01 
GeneralNodes empty Pin
superbloki10-Aug-09 2:49
superbloki10-Aug-09 2:49 
GeneralRe: Nodes empty Pin
dan_bock7-Oct-09 6:11
dan_bock7-Oct-09 6:11 
GeneralRe: Nodes empty Pin
Hao M. Nguyen16-Oct-15 11:56
Hao M. Nguyen16-Oct-15 11:56 
GeneralTreeView with mouse click Pin
Osada Wijekoon7-Aug-09 19:59
Osada Wijekoon7-Aug-09 19:59 
GeneralThank You!! Pin
iccb101321-Jul-09 22:57
iccb101321-Jul-09 22:57 
Questioncan use vb.net code Pin
bas2008_5720-Jul-09 22:42
bas2008_5720-Jul-09 22:42 
QuestionHow to make font of the Node Text to bold? Pin
Vivek_Minhas8-Jul-09 19:50
Vivek_Minhas8-Jul-09 19:50 
NewsAdvanced TreeView version 1.7.0.0 code available on source forge Pin
Vivek_Minhas8-Jul-09 19:24
Vivek_Minhas8-Jul-09 19:24 
QuestionText Wrapping in Column Pin
Stewart McGuire3-Jun-09 16:19
Stewart McGuire3-Jun-09 16:19 
AnswerRe: Text Wrapping in Column Pin
Paul Selormey2-Jul-09 12:13
Paul Selormey2-Jul-09 12:13 

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.