Click here to Skip to main content
15,861,168 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

 
AnswerRe: How to automatically select all childs in a node if the parent checkbox is selected Pin
SeaWater3-Jun-10 4:25
SeaWater3-Jun-10 4:25 
QuestionHow to add items into this Advanced TreeView having columns Pin
tamour ahmad28-Apr-10 3:33
tamour ahmad28-Apr-10 3:33 
AnswerRe: How to add items into this Advanced TreeView having columns Pin
DAvenLo1-May-10 22:20
DAvenLo1-May-10 22:20 
GeneralQuestion Pin
thready19-Apr-10 18:49
thready19-Apr-10 18:49 
GeneralRe: Question Pin
DAvenLo26-Apr-10 2:05
DAvenLo26-Apr-10 2:05 
AnswerRe: Question [modified] Pin
DAvenLo1-May-10 21:54
DAvenLo1-May-10 21:54 
GeneralRe: Question Pin
thready2-May-10 3:38
thready2-May-10 3:38 
GeneralRe: Question [modified] Pin
DAvenLo3-May-10 20:17
DAvenLo3-May-10 20:17 
QuestionHow can i change BackgroundBrush in other columns than first? Pin
marcin.swi6-Apr-10 21:22
marcin.swi6-Apr-10 21:22 
QuestionIs it possible to display bitmaps in columns? Pin
usg4-Mar-10 11:56
usg4-Mar-10 11:56 
GeneralError in TreeViewAdv Pin
blacksaifer24-Feb-10 15:35
blacksaifer24-Feb-10 15:35 
QuestionCheckboxes are not in the correct position when moving columns Pin
Member 167849515-Jan-10 7:00
Member 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
turinreza14-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
turinreza14-Jan-10 10:13 
Question[My vote of 1] How do I enable tri-state checkboxes? Pin
Mike Martell18-Dec-09 9:31
Mike Martell18-Dec-09 9:31 
Generalnodecontrol textinput box mapped to int value of datarow Pin
turinreza16-Dec-09 2:16
turinreza16-Dec-09 2:16 
Questioncheckbox cant check iwhen using Columns? Pin
turinreza14-Dec-09 12:42
turinreza14-Dec-09 12:42 
AnswerRe: checkbox cant check iwhen using Columns? Pin
keyboarder29-Aug-13 11:00
professionalkeyboarder29-Aug-13 11:00 
QuestionAssociate a context menu with each NodeControl of a TreeViewAdv : possible ? Pin
michel_page3821-Nov-09 10:04
michel_page3821-Nov-09 10:04 
GeneralLatest code no worky Pin
Michael Janulaitis19-Nov-09 7:21
Michael Janulaitis19-Nov-09 7:21 
GeneralImproved GetRowAt(Point point) Pin
YevgenBorodkin18-Nov-09 0:05
YevgenBorodkin18-Nov-09 0:05 
AnswerImproved AutoSizeColumn function Pin
ub3rst4r8-Nov-09 8:54
ub3rst4r8-Nov-09 8:54 
GeneralRe: Improved AutoSizeColumn function Pin
Steve Towner14-Nov-09 17:15
Steve Towner14-Nov-09 17:15 
GeneralRe: Improved AutoSizeColumn function Pin
YevgenBorodkin17-Nov-09 23:49
YevgenBorodkin17-Nov-09 23:49 
GeneralRe: Improved AutoSizeColumn function Pin
Summersyu12-Jul-11 3:34
Summersyu12-Jul-11 3:34 

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.