Drag and Drop of treenode in a treeview
First Call the ItemDrag event of the source tree view and call the DoDrag method within that event. Then call the DragEnter event of the destination tree view. Finally, add the following code to the DragDrop event of the destination tree view.
//------Add this code in the DragDrop Event------
TreeNode trNode = null;
if(e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
{
Point objPoint = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode trNodeDestination = ((TreeView)sender).GetNodeAt(objPoint);
//If no node from the destination tree view is selected then add the selected node to the
//last node of the destination tree view.
if (trNodeDestination == null)
{
trNodeDestination = ((TreeView)sender).Nodes[((TreeView)sender).Nodes.Count - 1].LastNode;
}
trNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
if (trNodeDestination.TreeView != trNode.TreeView)
{
trNodeDestination.Nodes.Add((TreeNode)trNode.Clone());
trNodeDestination.Expand();
//Remove Original Node
trNode.Remove();
}
}
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil