Click here to Skip to main content
15,902,797 members
Articles / Programming Languages / Visual Basic
Article

TreeView Rearrange

Rate me:
Please Sign up or sign in to vote.
4.87/5 (57 votes)
6 Jun 2005Public Domain4 min read 248.2K   4.7K   132   80
How to do a TreeView rearrange.

Introduction

While merrily programming a top secret (snicker) application I was developing - I ran into a major roadblock that set me back a few days. My application was based around a treeview but I wanted to give users the ability to rearrange the node/folder structure of the treeview using drag and drop. Upon a bit of research, I found the treeview does have some sort of drag & drop support, but as far as I could tell, this just turned on the event handlers for when a user starts dragging a node. "Hmmm", I thought, "this is gonna require some custom coding."... Let's delve into my series of failures and successes leading us to a workable extension of the treeview control.

How is it going to work?

In my first attempt, I wanted all the visual goodies. I wanted the dragged node to be placed into the "slots" in between nodes as I moved over them. This, unfortunately, led me to some problems. When the node moves into a slot - the treeview structure changes and the node your mouse is currently over changes entirely, this introduced some (only slightly entertaining) node dances. I then decided to remove the node being dragged, to prevent the node dance - and it worked! My next problem was that I wanted to be able to drag the node into a "folder" node but what if the folder node is closed? I'm sure you guessed it, use Expand()! But guess what!? The dreaded node dance returned! :(

I gave up for the night and went to bed without any hope and (like many a times) woke up with an answer. Mimic the "Windows" (tm) way and implement a placeholder letting the user know where the item will be dropped. With a bit of work and a few cases of RC cola, I had the functionality I wanted.

Moving on to the code

Most of the functionality is handled within the "DragOver" event of the treeview control, however there are two other events that needed to be handled to give you the cursor effect you need.

C#
private void treeView1_ItemDrag(object sender, 
  System.Windows.Forms.ItemDragEventArgs e)
{
    DoDragDrop(e.Item, DragDropEffects.Move);
}


private void treeView1_DragEnter(object sender, 
  System.Windows.Forms.DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

Now into the DragOver event. The point to remember here is that we're dealing with two types of nodes: those that can accept children and those that can't. For nodes that can accept children, we need to have three vertical regions we have to watch for:

  • Top (placeholder above)
  • Bottom (placeholder below)
  • Middle (expand the folder and allow users to drop nodes onto it)

The non-folder nodes are divided into two vertical regions. For brevity, I will only go into the code for the "top" of the "non-folder" node. This code is divided into four main parts:

  • Paradox prevention, we should never be allowed to drag a folder into a child folder.
  • Store the current placeholder into a global string called NodeMap, basically a pipe delimited set of indexes. If the new NodeMap matches the global NodeMap then method returns to minimize flickering.
  • Refresh the screen to remove old placeholders.
  • Draw the placeholders.
C#
#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
    if(tnParadox.Parent == NodeMoving)
    {
        this.NodeMap = "";
        return;
    }
    
    tnParadox = tnParadox.Parent;
}
#endregion
#region Store the placeholder info into a pipe delimited string
TreeNode tnPlaceholderInfo = NodeOver;
string NewNodeMap = ((int)NodeOver.Index).ToString();
while(tnPlaceholderInfo.Parent != null)
{
    tnPlaceholderInfo = tnPlaceholderInfo.Parent;
     NewNodeMap = tnPlaceholderInfo.Index + "|" + NewNodeMap;
}
if(NewNodeMap == this.NodeMap)
    return;
else
    this.NodeMap = NewNodeMap;
#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the placeholders
int LeftPos, RightPos;
LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth;
RightPos = this.treeView1.Width - 4;
Point[] LeftTriangle = new Point[5]{
    new Point(LeftPos, NodeOver.Bounds.Top - 4), 
    new Point(LeftPos, NodeOver.Bounds.Top + 4), 
    new Point(LeftPos + 4, NodeOver.Bounds.Y), 
    new Point(LeftPos + 4, NodeOver.Bounds.Top - 1), 
    new Point(LeftPos, NodeOver.Bounds.Top - 5)};

Point[] RightTriangle = new Point[5]{
    new Point(RightPos, NodeOver.Bounds.Top - 4),
    new Point(RightPos, NodeOver.Bounds.Top + 4),
    new Point(RightPos - 4, NodeOver.Bounds.Y),
    new Point(RightPos - 4, NodeOver.Bounds.Top - 1),
    new Point(RightPos, NodeOver.Bounds.Top - 5)};

g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle);
g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle);
g.DrawLine(new System.Drawing.Pen(Color.Black, 2), 
  new Point(LeftPos, NodeOver.Bounds.Top), 
  new Point(RightPos, NodeOver.Bounds.Top));
#endregion

The placeholders are drawn with three graphics calls to create the black line and two triangles.

The final piece to the puzzle is actually moving the dragged node. This is handled in the drag_drop event. It basically traverses the nodemap, adds the new node and removes the old one.

C#
private void treeView1_DragDrop(object sender, 
  System.Windows.Forms.DragEventArgs e)

{
    if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode",
      false) && this.NodeMap != "")
    { 
        TreeNode MovingNode = (TreeNode)e.Data.GetData(
          "System.Windows.Forms.TreeNode");
        string[] NodeIndexes = this.NodeMap.Split('|');
        TreeNodeCollection InsertCollection = this.treeView1.Nodes;
        for(int i = 0; i < NodeIndexes.Length - 1; i++)
        {
            InsertCollection = InsertCollection[Int32.Parse(
               NodeIndexes[i])].Nodes;
        }
        if(InsertCollection != null)
        {
            InsertCollection.Insert(Int32.Parse(NodeIndexes[
             NodeIndexes.Length - 1]), (TreeNode)MovingNode.Clone());
            this.treeView1.SelectedNode = InsertCollection[
             Int32.Parse(NodeIndexes[NodeIndexes.Length - 1])];
            MovingNode.Remove();
        }
    } 

}

Remarks

While not a completely comprehensive solution, I believe this article will be a good stepping stone for those wanting to add better drag and drop support to their applications. The code is divided clearly into regions and commented fairly well.

I look forward to your comments and suggestions. CodeProject is a great site, and I'm glad to contribute.

History

  • February 11, 2004: (Version 1.0) code and article submitted.
  • March 7, 2004: (Version 1.1) minor bug where the last subfolder could be dragged onto itself fixed.
  • June, 5, 2005 (Version 1.2) utilized code by creatio, needed some minor tweaks but got it working. Also made an attempt to convert the code into VB.NET - there is a problem with the code - see comment that says "Error!". Not sure what is going on because I'm not a very competent VB.NET developer - advice appreciated.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
United States United States
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 can I darg-drop item to a WebBrowser control ? Pin
Gabe Anguiano16-Sep-05 7:53
Gabe Anguiano16-Sep-05 7:53 
GeneralPossible in VC++ Pin
smile869123-Aug-05 9:45
smile869123-Aug-05 9:45 
GeneralRe: Possible in VC++ Pin
Gabe Anguiano23-Aug-05 9:46
Gabe Anguiano23-Aug-05 9:46 
GeneralRe: Possible in VC++ Pin
smile869123-Aug-05 9:48
smile869123-Aug-05 9:48 
GeneralTreeView Rearrange Pin
smile86919-Aug-05 4:27
smile86919-Aug-05 4:27 
GeneralNice code sample Pin
Emiliano9-Jun-05 1:48
Emiliano9-Jun-05 1:48 
GeneralRe: Nice code sample Pin
Gabe Anguiano9-Jun-05 5:55
Gabe Anguiano9-Jun-05 5:55 
GeneralSome notes. Pin
creatio26-May-05 0:03
creatio26-May-05 0:03 
Hi.

Good description and nice code made it realy usable and valuable.
However, while analyzing your code I spotted several minor bugs.

1. The drag'n'drop is dependant on the image index of the TreeNode thus preventing us from using different images on different levels (or branches)of the tree.

2. You have used the type string for your NewNodeMap variable, which lead to the frequent memory reallocations, while filling this map. This lead to the unpleasant delays.

3. Many peaces of the code are duplicated about four times (!).

Of course it wouldn't be a comprehansive message if only I wouldn't added my own version of this bugs fixes. I didn't tested it much, but I fixed bugs 2 and 3 and felt myself a bit lazy for the first one Smile | :)

May be my code will inspire you to fix even the first mentioned bug. Wink | ;)

private const int MAPSIZE = 128;
private StringBuilder NewNodeMap = new StringBuilder(MAPSIZE);

private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeNode NodeOver = this.treeView1.GetNodeAt(this.treeView1.PointToClient(Cursor.Position));
TreeNode NodeMoving = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

// A bit long, but to summarize, process the following coe only if the nodeover is null
// and either the nodeover is not the same thing as nodemoving UNLESSS nodeover happens
// to be the last node in the branch (so we can allow drag & drop below a parent branch)
if(NodeOver != null && (NodeOver != NodeMoving || (NodeOver.Parent != null && NodeOver.Index == (NodeOver.Parent.Nodes.Count - 1))))
{
int OffsetY = this.treeView1.PointToClient(Cursor.Position).Y - NodeOver.Bounds.Top;

//////////////////////////////////////////////////////////////////////////
//BAD!!!! Can't use images other then with ImageIndex == 1 for the folders
//////////////////////////////////////////////////////////////////////////
// Image index of 1 is the non-folder icon
if(NodeOver.ImageIndex == 1)
{
#region Standard Node
if(OffsetY < (NodeOver.Bounds.Height / 2))
{
//this.lblDebug.Text = "top";

#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
if(tnParadox.Parent == NodeMoving)
{
this.NodeMap = "";
return;
}

tnParadox = tnParadox.Parent;
}
#endregion
#region Store the placeholder info into a pipe delimited string
SetNewNodeMap(NodeOver);
if(SetMapsEqual() == true)
return;

#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the placeholders
DrawLeafTopPlaceholders(NodeOver);
#endregion
}
else
{
//this.lblDebug.Text = "bottom";

#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
if(tnParadox.Parent == NodeMoving)
{
this.NodeMap = "";
return;
}

tnParadox = tnParadox.Parent;
}
#endregion
#region Allow drag drop to parent branches
TreeNode ParentDragDrop = null;
// If the node the mouse is over is the last node of the branch we should allow
// the ability to drop the "nodemoving" node BELOW the parent node
if(NodeOver.Parent != null && NodeOver.Index == (NodeOver.Parent.Nodes.Count - 1))
{
int XPos = this.treeView1.PointToClient(Cursor.Position).X;
if(XPos < NodeOver.Bounds.Left)
{
ParentDragDrop = NodeOver.Parent;
while(true)
{
if(XPos > (ParentDragDrop.Bounds.Left -
this.treeView1.ImageList.Images[
ParentDragDrop.ImageIndex].Size.Width))
break;

if(ParentDragDrop.Parent != null)
ParentDragDrop = ParentDragDrop.Parent;
else
break;
}
}
}
#endregion
#region Store the placeholder info into a pipe delimited string
// Since we are in a special case here, use the ParentDragDrop node as the current "nodeover"
TreeNode tnPlaceholderInfo = ParentDragDrop != null ?
ParentDragDrop : NodeOver;

SetNewNodeMap(tnPlaceholderInfo);
if(SetMapsEqual() == true)
return;

#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the placeholders
DrawLeafBottomPlaceholders(NodeOver, ParentDragDrop);
#endregion
}
#endregion
}
else
{
#region Folder Node
if(OffsetY < (NodeOver.Bounds.Height / 3))
{
//this.lblDebug.Text = "folder top";

#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
if(tnParadox.Parent == NodeMoving)
{
this.NodeMap = "";
return;
}

tnParadox = tnParadox.Parent;
}
#endregion
#region Store the placeholder info into a pipe delimited string
SetNewNodeMap(NodeOver);
if(SetMapsEqual() == true)
return;
#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the placeholders
#endregion
}
else if((NodeOver.Parent != null && NodeOver.Index == 0) &&
(OffsetY > (NodeOver.Bounds.Height - (NodeOver.Bounds.Height / 3))))
{
//this.lblDebug.Text = "folder bottom";

#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
if(tnParadox.Parent == NodeMoving)
{
this.NodeMap = "";
return;
}

tnParadox = tnParadox.Parent;
}
#endregion
#region Store the placeholder info into a pipe delimited string
SetNewNodeMap(NodeOver);
if(SetMapsEqual() == true)
return;
#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the placeholders
DrawFolderTopPlaceholders(NodeOver);
#endregion
}
else
{
//this.lblDebug.Text = "folder over";

if(NodeOver.Nodes.Count > 0)
{
NodeOver.Expand();
//this.Refresh();
}
else
{
#region Prevent the node from being dragged onto itself
if(NodeMoving == NodeOver)
return;
#endregion
#region If NodeOver is a child then cancel
TreeNode tnParadox = NodeOver;
while(tnParadox.Parent != null)
{
if(tnParadox.Parent == NodeMoving)
{
this.NodeMap = "";
return;
}

tnParadox = tnParadox.Parent;
}
#endregion
#region Store the placeholder info into a pipe delimited string
SetNewNodeMap(NodeOver);
NewNodeMap = NewNodeMap.Insert(NewNodeMap.Length, "|0");

if(SetMapsEqual() == true)
return;
#endregion
#region Clear placeholders above and below
this.Refresh();
#endregion
#region Draw the "add to folder" placeholder
DrawAddToFolderPlaceholder(NodeOver);
#endregion
}
}
#endregion
}
}
}//eom

private void DrawLeafTopPlaceholders(TreeNode NodeOver)
{
Graphics g = this.treeView1.CreateGraphics();

int NodeOverImageWidth = this.treeView1.ImageList.Images[NodeOver.ImageIndex].Size.Width + 8;
int LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth;
int RightPos = this.treeView1.Width - 4;

Point[] LeftTriangle = new Point[5]{
new Point(LeftPos, NodeOver.Bounds.Top - 4),
new Point(LeftPos, NodeOver.Bounds.Top + 4),
new Point(LeftPos + 4, NodeOver.Bounds.Y),
new Point(LeftPos + 4, NodeOver.Bounds.Top - 1),
new Point(LeftPos, NodeOver.Bounds.Top - 5)};

Point[] RightTriangle = new Point[5]{
new Point(RightPos, NodeOver.Bounds.Top - 4),
new Point(RightPos, NodeOver.Bounds.Top + 4),
new Point(RightPos - 4, NodeOver.Bounds.Y),
new Point(RightPos - 4, NodeOver.Bounds.Top - 1),
new Point(RightPos, NodeOver.Bounds.Top - 5)};


g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle);
g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle);
g.DrawLine(new System.Drawing.Pen(Color.Black, 2), new Point(LeftPos, NodeOver.Bounds.Top), new Point(RightPos, NodeOver.Bounds.Top));

}//eom

private void DrawLeafBottomPlaceholders(TreeNode NodeOver, TreeNode ParentDragDrop)
{
Graphics g = this.treeView1.CreateGraphics();

int NodeOverImageWidth = this.treeView1.ImageList.Images[NodeOver.ImageIndex].Size.Width + 8;
// Once again, we are not dragging to node over, draw the placeholder using the ParentDragDrop bounds
int LeftPos, RightPos;
if(ParentDragDrop != null)
LeftPos = ParentDragDrop.Bounds.Left - (this.treeView1.ImageList.Images[ParentDragDrop.ImageIndex].Size.Width + 8);
else
LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth;
RightPos = this.treeView1.Width - 4;

Point[] LeftTriangle = new Point[5]{
new Point(LeftPos, NodeOver.Bounds.Bottom - 4),
new Point(LeftPos, NodeOver.Bounds.Bottom + 4),
new Point(LeftPos + 4, NodeOver.Bounds.Bottom),
new Point(LeftPos + 4, NodeOver.Bounds.Bottom - 1),
new Point(LeftPos, NodeOver.Bounds.Bottom - 5)};

Point[] RightTriangle = new Point[5]{
new Point(RightPos, NodeOver.Bounds.Bottom - 4),
new Point(RightPos, NodeOver.Bounds.Bottom + 4),
new Point(RightPos - 4, NodeOver.Bounds.Bottom),
new Point(RightPos - 4, NodeOver.Bounds.Bottom - 1),
new Point(RightPos, NodeOver.Bounds.Bottom - 5)};


g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle);
g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle);
g.DrawLine(new System.Drawing.Pen(Color.Black, 2), new Point(LeftPos, NodeOver.Bounds.Bottom), new Point(RightPos, NodeOver.Bounds.Bottom));
}//eom

private void DrawFolderTopPlaceholders(TreeNode NodeOver)
{
Graphics g = this.treeView1.CreateGraphics();
int NodeOverImageWidth = this.treeView1.ImageList.Images[NodeOver.ImageIndex].Size.Width + 8;

int LeftPos, RightPos;
LeftPos = NodeOver.Bounds.Left - NodeOverImageWidth;
RightPos = this.treeView1.Width - 4;

Point[] LeftTriangle = new Point[5]{
new Point(LeftPos, NodeOver.Bounds.Top - 4),
new Point(LeftPos, NodeOver.Bounds.Top + 4),
new Point(LeftPos + 4, NodeOver.Bounds.Y),
new Point(LeftPos + 4, NodeOver.Bounds.Top - 1),
new Point(LeftPos, NodeOver.Bounds.Top - 5)};

Point[] RightTriangle = new Point[5]{
new Point(RightPos, NodeOver.Bounds.Top - 4),
new Point(RightPos, NodeOver.Bounds.Top + 4),
new Point(RightPos - 4, NodeOver.Bounds.Y),
new Point(RightPos - 4, NodeOver.Bounds.Top - 1),
new Point(RightPos, NodeOver.Bounds.Top - 5)};


g.FillPolygon(System.Drawing.Brushes.Black, LeftTriangle);
g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle);
g.DrawLine(new System.Drawing.Pen(Color.Black, 2), new Point(LeftPos, NodeOver.Bounds.Top), new Point(RightPos, NodeOver.Bounds.Top));

}//eom
private void DrawAddToFolderPlaceholder(TreeNode NodeOver)
{
Graphics g = this.treeView1.CreateGraphics();
int RightPos = NodeOver.Bounds.Right + 6;
Point[] RightTriangle = new Point[5]{
new Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) + 4),
new Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) + 4),
new Point(RightPos - 4, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2)),
new Point(RightPos - 4, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) - 1),
new Point(RightPos, NodeOver.Bounds.Y + (NodeOver.Bounds.Height / 2) - 5)};

this.Refresh();
g.FillPolygon(System.Drawing.Brushes.Black, RightTriangle);
}//eom


private void SetNewNodeMap(TreeNode tnNode)
{
NewNodeMap.Length = 0;
NewNodeMap.Insert(0, (int)tnNode.Index);

TreeNode tnCurNode = tnNode;

while(tnCurNode.Parent != null)
{
tnCurNode = tnCurNode.Parent;
NewNodeMap.Insert(0, tnCurNode.Index + "|");
}
}//oem

private bool SetMapsEqual()
{
if(this.NewNodeMap.ToString() == this.NodeMap)
return true;
else
{
this.NodeMap = this.NewNodeMap.ToString();
return false;
}
}//oem



By the way, thanks for a good job!
GeneralRe: Some notes. Pin
Gabe Anguiano26-May-05 5:32
Gabe Anguiano26-May-05 5:32 
GeneralRe: Some notes. Pin
creatio26-May-05 6:08
creatio26-May-05 6:08 
GeneralRe: Some notes. Pin
B. ten Hoopen21-Apr-06 0:26
B. ten Hoopen21-Apr-06 0:26 
QuestionRe: Some notes. [modified] Pin
marcogaribay7-Jun-06 7:16
marcogaribay7-Jun-06 7:16 
AnswerRe: Some notes. [modified] Pin
B. ten Hoopen7-Jun-06 23:22
B. ten Hoopen7-Jun-06 23:22 
GeneralAquire the target node Pin
Member 172456416-May-05 23:15
Member 172456416-May-05 23:15 
GeneralRe: Aquire the target node Pin
Gabe Anguiano5-Jun-05 10:45
Gabe Anguiano5-Jun-05 10:45 
GeneralRe: Aquire the target node Pin
Gabe Anguiano5-Jun-05 10:46
Gabe Anguiano5-Jun-05 10:46 
GeneralVisual Basic Pin
Member 19069964-May-05 22:19
Member 19069964-May-05 22:19 
GeneralRe: Visual Basic Pin
Gabe Anguiano5-Jun-05 12:48
Gabe Anguiano5-Jun-05 12:48 
GeneralRe: Visual Basic Pin
Member 19069965-Jun-05 12:52
Member 19069965-Jun-05 12:52 
GeneralThanks Pin
prog329-Mar-05 4:27
prog329-Mar-05 4:27 
GeneralRe: Thanks Pin
Gabe Anguiano11-Mar-05 7:50
Gabe Anguiano11-Mar-05 7:50 
QuestionNice .. Possible in VB? Pin
MightyMart4-Nov-04 14:35
MightyMart4-Nov-04 14:35 
AnswerYes - here is a conversion to VB Pin
Member 151841125-Nov-04 8:19
Member 151841125-Nov-04 8:19 
GeneralRe: Yes - here is a conversion to VB Pin
Member 15184114-Dec-04 4:46
Member 15184114-Dec-04 4:46 
GeneralRe: Yes - here is a conversion to VB Pin
Gabe Anguiano26-May-05 5:33
Gabe Anguiano26-May-05 5:33 

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.