Click here to Skip to main content
6,291,124 members and growing! (17,193 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, WinForms, VS2005, Dev
Posted:10 Jul 2006
Views:174,742
Bookmarked:311 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
97 votes for this article.
Popularity: 9.53 Rating: 4.80 out of 5
2 votes, 2.1%
1

2
3 votes, 3.1%
3
6 votes, 6.3%
4
85 votes, 88.5%
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


Member

Occupation: Software Developer
Location: Russian Federation Russian Federation

Other popular Tree Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 142 (Total in Forum: 142) (Refresh)FirstPrevNext
QuestionText Wrapping in Column PinmemberStewart McGuire17:19 3 Jun '09  
AnswerRe: Text Wrapping in Column PinsupporterPaul Selormey13:13 2 Jul '09  
GeneralRed "X" and exception "Object is currently used elswhere" Pinmembermehafi22:41 27 May '09  
Questionhow do I select a node in FolderBrowserModel example? PinmemberKendoTM3:25 13 May '09  
QuestionDynamically change column width Pinmemberub3rst4r18:07 30 Apr '09  
GeneralDo you have any control with all these features but good for ASP.net PinmemberAmit Diwan12:34 28 Apr '09  
GeneralWhy Can't show My insert TreenodeAdv(Multi Tree Column) Pinmemberwangbairan1:32 13 Apr '09  
GeneralFont Color Pinmembergadz-ass3:38 10 Apr '09  
QuestionSimple question about icons PinmemberMember 33578489:59 2 Apr '09  
QuestionHow do you get the changed text on scroll? Pinmemberbob.cohn4:34 2 Apr '09  
GeneralHow to select a specific node? PinmemberQwertie7:03 27 Mar '09  
GeneralRe: How to select a specific node? PinmemberQwertie7:32 27 Mar '09  
GeneralConcrats PinmemberMember 205557311:07 24 Mar '09  
GeneralThanks PinmemberMember 16784954:32 19 Mar '09  
GeneralTreeViewAdv for Windows Mobile PinmemberSzorcc23:10 11 Mar '09  
GeneralRe: TreeViewAdv for Windows Mobile PinmemberQwertie7:06 27 Mar '09  
GeneralTreeViewAdv reads children when it shouldn't [modified] PinmemberQwertie9:10 19 Feb '09  
GeneralNodeDecimalTextBox has a protected constructor. Is this by design? Pinmembermgrounds4:53 16 Feb '09  
GeneralRe: NodeDecimalTextBox has a protected constructor. Is this by design? PinsupporterPaul Selormey13:10 2 Jul '09  
QuestionFile filter PinmemberWicksmeister2:06 1 Feb '09  
GeneralPossible to set column height? Pinmembervigylant7:41 26 Jan '09  
GeneralVB example with table from database? PinmemberDavor5:31 26 Jan '09  
Generaldifferent icons for each node level Pinmemberskyline9221:40 25 Jan '09  
GeneralAn updated version of this project is available at SourceForge Pinmemberbscaer7:15 21 Jan '09  
GeneralRe: An updated version of this project is available at SourceForge PinmemberVivek_Minhas21:01 21 Jan '09  

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-2009
Web15 | Advertise on the Code Project