DragDrop in TreeView

shabbir's Avatar author of DragDrop in TreeView
This is an article on DragDrop in TreeView in C#.
Rated 5.00 By 1 users
When I started on Drag and Drop in C# I could not find much on the net and so I have decided to tackle the problem here. First follow the steps to see the working sample.

1. Create a new C sharp windows application. Rename Form1 to a suitable name. I would refer it as TestForm
2. Add a treeview control with some dummy nodes randomly.
3. Extract DragDropTreeView.cs from the attached zipped file and copy DragDropTreeView.cs to the project directory.
4. Add DragDropTreeView.cs as an Existing item to the project
5. Go to TestForm.Designer.cs
6. Add
Code: CSHARP
using DragDropTreeSample;
at the top of the file.
7. Now find
Code: CSHARP
private System.Windows.Forms.TreeView treeView1;
and replace with
Code: CSHARP
private DragDropTreeView treeView1;
8. Go to InitializeComponent function and find
Code: CSHARP
this.treeView1 = new System.Windows.Forms.TreeView();
and replace with
Code: CSHARP
this.treeView1 = new DragDropTreeView();
Thats it. Simple isnt it. All done and now you will see that your tree view control has the drag and drop facility with the nodes you have added. Use Ctrl Key to copy a node or move the nodes in the tree using mouse drag.

Now the question comes is what is in DragDropTreeView.cs so that it behaves in such a fashion.

First thing that it does is sets the property AllowDrop of the DragDropTreeView to true so that we can handle the drag drop events for the TreeView control.

Next it overrides the 3 basic or you can say minimal drag drop event handler of TreeView. i.e. OnDragDrop, OnDragOver, OnItemDrag
Code: CSHARP
protected override void OnItemDrag(ItemDragEventArgs e)
{
    srcNodeForDrag = (TreeNode)e.Item;
    this.DoDragDrop(e.Item, DragDropEffects.All);
}
When any item is dragged we store the source node so that we can operate on the node and then start the DragDrop operation.
Code: CSHARP
protected override void OnDragOver(DragEventArgs e)
{
    if ((e.KeyState & 8) == 8 &&
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy)
    {
        // CTL KeyState for copy.
        e.Effect = DragDropEffects.Copy;
    }
    else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)
    {
        e.Effect = DragDropEffects.Move;
    }
}
When any item is drag over other item we check the keyboard if we CTRL is pressed we set the copy mode of the nodes or else its the move mode.
Code: CSHARP
protected override void OnDragDrop(DragEventArgs e)
{
    Point pt = PointToClient(new Point(e.X, e.Y));
    TreeNode DestinationNode = this.GetNodeAt(pt);

    // Ensure that the list item index is contained in the data.
    if (e.Data.GetDataPresent(typeof(TreeNode)))
    {
        // Perform drag-and-drop, depending upon the effect.
        if (e.Effect == DragDropEffects.Copy)
        {
            TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
            DestinationNode.Nodes.Add(tmpTN);
        }
        else if (e.Effect == DragDropEffects.Move)
        {
            TreeNode tmpTN = (TreeNode)srcNodeForDrag.Clone();
            DestinationNode.Nodes.Add(tmpTN);
            srcNodeForDrag.Remove();
        }
    }
}
Here we check the Data type of the drop element and depending on the operation in the drag over we copy/move the node at the destination node.
Attached Files
File Type: zip DragDropTreeView.zip (908 Bytes, 235 views)
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
Dear Sir,

I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

I need this code very urgent.I will be waiting for your reply......

Thanking You Sir........
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
Dear Sir,

I have seen your code regarding drag and drop operations in a treeview.It is adding as a child at a dragged position.But i need to move nodes up and down with in a same level(drag and dropping of nodes at required position at a same level).

I need this code very urgent.I will be waiting for your reply......

Thanking You Sir........
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Do you mean you need to add the node as its siblings to the node where the drop has taken place.

If thats the case modify OnDragDrop to find the parent node of the DestinationNode and add it to the child of the parent node.
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
Dear Sir,

I am very thankful for giving the suggestion.


My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level.

OR

I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down.


I will be waiting for your esteemed reply........


Thanking You Sir........
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
--------------------------------------------------------------------------------

Dear Sir,

I am very thankful for giving the suggestion.


My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level with in a single tree.

OR

I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down with in a single tree.


I will be waiting for your esteemed reply........


Thanking You Sir........
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by viswa56@gmail.com
My problem is to add the dragged node as a next node(immediate sibling),at dropping position which are at same level with in a single tree.
After you get the parent where you would like to add node you can do that easily. Just try it out.
Quote:
Originally Posted by viswa56@gmail.com
I need code,such that when we a right-click on a node,the context menu has controls for moving the selected node up and down with in a single tree.
The above case also applies to the nodes to move using the menu or something similar.
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
Dear Sir,

With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


I want help on this context.


Please kindly reply me.

I will be waiting for your reply.......


Thanking You Sir.........
0
viswa56@gmail.com's Avatar, Join Date: Nov 2006
Go4Expert Member
--------------------------------------------------------------------------------

Dear Sir,

With help of your advice ,I can move the node.But it is adding as a last child to the parent. I want to place the dragged node at dropping position itself.


I want help on this context.


Please kindly reply me.

I will be waiting for your reply.......


Thanking You Sir.........
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You need to move the node to the needed position as Add does not have any overloaded which can add a specified position of the parent node.