Click here to Skip to main content
13,299,233 members (42,683 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 5 Apr 2013

TreeListViewEx Tree, List, and Drag and Drop

, 6 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Merged two CodeProject articles to make a better control, the TreeListViewEx.


A personal project to improve the user interface to sort Ordered Tests in Visual Studio required me to have a TreeListView. I started by searching the Internet to find a free control that I could use. I figured out some amazing controls, but the best for me was TreeListView by Thomas Caudal. That control worked fine, supports columns, edit, checkboxes, etc. One of my requirements is support drag and drop functionality between different TreeListViews to move items between lists and order those, unfortunately (maybe fortunately otherwise I would not write this article) the TreeListView did not support that functionality. One more time, I searched for a free control that could satisfy my expectation but no luck. So I decided to implement that functionality by myself using as base Thomas’s TreeListView. To achieve that, I searched again and that pointed me towards Drag and Drop List View by Matt Hawley. His control allows to reorder items in a ListView and supports moving to other ListViews indicating the position where the item in moving is going to be.

I decided to merge those two good jobs.

Let's Merge

I got both source codes, and I started renaming TreeListView to TreeListViewEx (use your own name is stimulant factor). After my first look at the implementation of TreeListView.cs. I fall into account it had more than 2 thousand lines of code. It is a lot of code and adding the support for drag and drop is going to be hard but no way, I got help from my friends the partial class, so for each class related with the TreeListView I am adding a partial class with the suffix .draganddrop.cs.

The Partial Class TreeListViewItem.draganddrop.cs

The drag and drop implementation works making a clone of each item to be moved. The clone is added to its new position and then the original item is removed from its source. The original implementation supported clone the ListView, my implementation has to support clone the TreeListViewItem and all its children.

public partial class TreeListViewItem 
    public override object Clone()
        var clone = new TreeListViewItem(Text, ImageIndex);

        for (int index = 1; index < SubItems.Count; index++)
            object subItem = SubItems[index];
            clone.SubItems.Add((ListViewSubItem) subItem);

        foreach (ICloneable item in Items)
            clone.Items.Add((TreeListViewItem) ((TreeListViewItem) item).Clone());

        clone.Group = Group;
        clone.IndentCount = IndentCount;
        clone.IsExpanded = IsExpanded;
        clone.ToolTipText = ToolTipText;
        clone.Checked = Checked;
        // Do not set the image index that does not work!
        //clone.ImageIndex = this.ImageIndex;

        clone.Tag = Tag;
        return clone;

The Partial Class TreeListView.draganddrop.cs

The implementation of this class was practically a copy paste of a "drag and drop” implementation renaming all ListView and ListViewItem by TreeListView and TreeListViewItem. So far the project compiles and runs but it has a lot of issues, because not all the implementation of TreeListView works with the drag and drop. So I had to fix all those weird behaviors and crashes with the debugger help (maybe it was the hardest part of this merge).

Fine Tuning the Drawing for Drag and Drop

The initial implementation for drag and drop drew a line indicating where the item is going to be moved this line cross the whole ListView. In the children of TreeListView, that line looked weird because a child has an indentation. Fixing the problem by resizing based on the indentation, we have the next:


It is interesting how open source can grow up. Without those previous implementations, I would not have achieved my target, or it would take a lot of time. Thanks to Thomas and Matt for their initiatives. Today I have a most complete implementation and nobody knows when somebody is going to take this code and improve it again.

Known Problems

  • No way to add items to an empty item. Who wants to add this functionality?


  • September, 2012: First implementation working


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Marco Medrano
Technical Lead InContact
Bolivia Bolivia
No Biography provided

You may also be interested in...

Comments and Discussions

Questionany idea why the BeforeLabelEdit event doesn't get fired? Pin
Member 93083822-Mar-14 0:26
memberMember 93083822-Mar-14 0:26 
AnswerRe: any idea why the BeforeLabelEdit event doesn't get fired? Pin
Marco Medrano4-Jun-14 7:35
professionalMarco Medrano4-Jun-14 7:35 
AnswerRe: any idea why the BeforeLabelEdit event doesn't get fired? Pin
Member 23073239-Jul-14 3:40
memberMember 23073239-Jul-14 3:40 
QuestionDoes not build Pin
sam.hill5-Apr-13 20:42
membersam.hill5-Apr-13 20:42 
AnswerRe: Does not build Pin
Marco Medrano6-Apr-13 20:05
professionalMarco Medrano6-Apr-13 20:05 
GeneralRe: Does not build Pin
sam.hill7-Apr-13 12:12
membersam.hill7-Apr-13 12:12 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 7 Apr 2013
Article Copyright 2013 by Marco Medrano
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid