Click here to Skip to main content
15,861,172 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.5K   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

 
BugDon't work in VS2017 Pin
Member 1105328012-Sep-23 3:59
Member 1105328012-Sep-23 3:59 
QuestionDoesn't work for me. Pin
Member 1492896911-May-23 8:17
Member 1492896911-May-23 8:17 
AnswerRe: Doesn't work for me. Pin
jzkirtley15-Jun-23 4:40
jzkirtley15-Jun-23 4:40 
QuestionMessage Removed Pin
16-Nov-20 5:22
f_tom16-Nov-20 5:22 
QuestionRight-clicking a node, Point-position of a given node. Pin
Member 133517048-Aug-17 3:53
Member 133517048-Aug-17 3:53 
AnswerRe: Right-clicking a node, Point-position of a given node. Pin
Member 1335170411-Aug-17 1:19
Member 1335170411-Aug-17 1:19 
QuestionHow to add the control to my project Pin
Member 1086838811-Apr-17 22:18
Member 1086838811-Apr-17 22:18 
AnswerRe: How to add the control to my project Pin
Black Horus12-Apr-17 7:40
Black Horus12-Apr-17 7:40 
Questionusing advtree in winform as a menu Pin
Member 129122883-Jan-17 8:06
Member 129122883-Jan-17 8:06 
QuestionTreeviewAdv Pin
ss011717-Mar-16 23:25
ss011717-Mar-16 23:25 
AnswerRe: TreeviewAdv Pin
IDC20-Jun-16 20:30
IDC20-Jun-16 20:30 
Questionhow to use this component? Pin
kedigemisi19-Jan-16 4:21
kedigemisi19-Jan-16 4:21 
AnswerRe: how to use this component? Pin
GaoYunpeng3-Apr-16 23:54
GaoYunpeng3-Apr-16 23:54 
QuestionHiding extra column Pin
Andrew Ellis1-Oct-15 0:36
Andrew Ellis1-Oct-15 0:36 
QuestionHas anyone worked out how to set the background color of an individual row? Pin
Kebrite18-Jul-15 7:10
Kebrite18-Jul-15 7:10 
AnswerRe: Has anyone worked out how to set the background color of an individual row? Pin
IDC20-Jun-16 20:28
IDC20-Jun-16 20:28 
Questioni just want to know how to use nodeComboBox PinPopular
mzxjhx23-Jun-15 3:10
mzxjhx23-Jun-15 3:10 
QuestionLooks great.. Pin
Mohan Sawant27-Jan-15 23:34
Mohan Sawant27-Jan-15 23:34 
QuestionGrouping Pin
Brendt.w8-Sep-14 0:03
Brendt.w8-Sep-14 0:03 
QuestionTreeview in Asp.Net Pin
Member 1102619821-Aug-14 19:56
Member 1102619821-Aug-14 19:56 
QuestionIn one PC, the tree node's text is not show. Pin
mail_main27-May-14 17:37
mail_main27-May-14 17:37 
QuestioniedEdit node Pin
Member 950575615-May-14 19:33
Member 950575615-May-14 19:33 
QuestionNodeButton Pin
HansDampft24-Mar-14 18:25
HansDampft24-Mar-14 18:25 
Hi,

did anyone already made a NodeButton? I really would like to have a Button to be displayed in my rows. If anyone did something like that or have some tips that would be cool.
I tried to do one and copied most of the code from the NodeComboBox and have rewritten it to return a new Forms.Button but it just displays a String...
Questionreceiving error on conversion Pin
agent_kruger22-Feb-14 7:34
professionalagent_kruger22-Feb-14 7:34 
QuestionisLeaf is always false, so leaf state icon does not display Pin
Member 1055910529-Jan-14 13:58
Member 1055910529-Jan-14 13:58 

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.