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

Stats

1.1M views
32.6K downloads
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

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

Share

About the Author

Andrey Gliznetsov
Software Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralCancel and disable Sorting Pin
cali_caligo5-Oct-10 2:46
Membercali_caligo5-Oct-10 2:46 
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 
How do you automatically select all childs under a parent node if the parent checkbox is selected? This must be a built in feature that I can't find how to do... Smile | :)

Thanks Before Hand,
SeaWater
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 
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 

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.