Click here to Skip to main content
Click here to Skip to main content

TreeListViewEx Tree, List, and Drag and Drop

By , 6 Apr 2013
 

  

Introduction 

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 and 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 did not write this article) the TreeListView did not support that functionality. One more time I searched by a free control that could satisfy my expectative but no lucky. So I decided 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 is going to be the item in moving.  

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 to 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, due that 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 due that a child has an indentation.  Fixing the problem by resizing based on the indentation we have the next:

Conclusions

Is interesting how open source can grow up. Without those previous implementations I would not achieved my target, or it would take a lot of time. Thanks to Thomas and Matt  for theirs initiative. 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 want add this functionality?

History

  • September, 2012: First implementation working.

License

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

About the Author

Marco Medrano
Team Leader Jalasoft
Bolivia Bolivia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDoes not buildmembersam.hill5 Apr '13 - 19:42 
AnswerRe: Does not buildprofessionalMarco Medrano6 Apr '13 - 19:05 
GeneralRe: Does not buildmembersam.hill7 Apr '13 - 11:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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