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
at the top of the file.
7. Now find
and replace with
8. Go to InitializeComponent function and find
and replace with
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
Next it overrides the 3 basic or you can say minimal drag drop event handler of TreeView. i.e.
When any item is dragged we store the source node so that we can operate on the node and then start the DragDrop operation.
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.
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.
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;
7. Now find
Code: CSHARP
private System.Windows.Forms.TreeView treeView1;
Code: CSHARP
private DragDropTreeView treeView1;
Code: CSHARP
this.treeView1 = new System.Windows.Forms.TreeView();
Code: CSHARP
this.treeView1 = new DragDropTreeView();
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, OnItemDragCode: CSHARP
protected override void OnItemDrag(ItemDragEventArgs e)
{
srcNodeForDrag = (TreeNode)e.Item;
this.DoDragDrop(e.Item, DragDropEffects.All);
}
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;
}
}
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();
}
}
}

