5,696,576 members and growing! (15,107 online)
Email Password   helpLost your password?
Desktop Development » Tree Controls » TreeView Controls     Intermediate

Advanced TreeView for .NET

By Andrey Gliznetsov

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.
C#, Windows, .NET 2.0, .NET, WinForms, Visual Studio, VS2005, Dev

Posted: 10 Jul 2006
Updated: 10 Jul 2006
Views: 132,466
Bookmarked: 238 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
80 votes for this Article.
Popularity: 9.11 Rating: 4.79 out of 5
2 votes, 2.5%
1
0 votes, 0.0%
2
2 votes, 2.5%
3
5 votes, 6.3%
4
70 votes, 88.6%
5

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:

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.

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

About the Author

Andrey Gliznetsov



Occupation: Software Developer
Location: Russian Federation Russian Federation

Other popular Tree Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 104 (Total in Forum: 104) (Refresh)FirstPrevNext
QuestionTriggering CheckBox eventmemberCashels5:44 16 Oct '08  
QuestionGetting an assertionmembertho_wa2:45 25 Sep '08  
Generalhow to directly add children to the controlmemberUnruled Boy6:24 6 Sep '08  
GeneralEditor data validatememberlrestell6:01 25 Aug '08  
GeneralSelect node programaticallymemberMiki Nanuk12:47 11 Aug '08  
AnswerRe: Select node programaticallymembers_mon23:03 11 Aug '08  
GeneralRe: Select node programaticallymemberMiki Nanuk6:17 13 Aug '08  
GeneralRe: Select node programaticallymembers_mon0:42 25 Aug '08  
QuestionBOLD textmemberNatalie_Dillon23:48 10 Aug '08  
GeneralCopy notes between two AdvTreeView?memberwebmimuel6:01 31 Jul '08  
GeneralRe: Copy notes between two AdvTreeView?members_mon0:10 11 Aug '08  
GeneralInsert Grid Linesmembernight_scorpion5:56 9 Jul '08  
GeneralHow can I begin editing of a node label without a mouse?membervano_by0:34 4 Jun '08  
GeneralText Color in nodemembermsrr10:49 9 May '08  
AnswerRe: Text Color in nodemembergewe4:16 14 May '08  
QuestionRe: Text Color in nodemembertho_wa4:16 29 Sep '08  
AnswerRe: Text Color in nodemembergewe0:06 18 Oct '08  
Generalhow can i add tooltip for each node?membermargiex1:16 28 Apr '08  
GeneralRe: how can i add tooltip for each node?memberCandace Bain7:25 25 Sep '08  
QuestionHorizontal scrolling: column header text not moving alongmembergewe3:58 16 Apr '08  
AnswerRe: Horizontal scrolling: column header text not moving alongmembergewe5:07 14 May '08  
GeneralWich is the simplest way to work with multicolumn?memberPuiuMT0:50 29 Feb '08  
GeneralRe: Wich is the simplest way to work with multicolumn? [modified]memberpj3456:54 2 Mar '08  
GeneralRe: Wich is the simplest way to work with multicolumn?memberPuiuMT7:19 3 Mar '08  
GeneralProblem Loading Solutionmemberfishindude5:33 27 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jul 2006
Editor: Smitha Vijayan
Copyright 2006 by Andrey Gliznetsov
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project