This blog will show you how you can perform drag and drop operations on treeview control node using C#.Net in Windows application. So for this article first we will create a new windows application and add the below code on form load.
  1. private void Form2_Load(object sender, EventArgs e)
  2. {
  3. treeView1 = new TreeView();
  4. this.SuspendLayout();
  5. // Initialize treeView1.
  6. treeView1.AllowDrop = true;
  7. treeView1.Dock = DockStyle.Fill;
  8. // Add nodes to treeView1.
  9. TreeNode node;
  10. for (int x = 0; x < 5; ++x)
  11. {
  12. // Add a root node to treeView1.
  13. node = treeView1.Nodes.Add(String.Format("Node{0}", x * 4));
  14. for (int y = 1; y < 6; ++y)
  15. {
  16. // Add a child node to the previously added node.
  17. node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y));
  18. }
  19. }
  20. // Add event handlers for the required drag events.
  21. treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);
  22. treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
  23. treeView1.DragOver += new DragEventHandler(treeView1_DragOver);
  24. treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);
  25. // Initialize the form.
  26. this.ClientSize = new Size(292, 273);
  27. this.Controls.Add(treeView1);
  28. this.ResumeLayout(false);
  29. }
In the above code I have created nodes of tree view. Now we will perform the operation for the drag and drop feature. First create the tree view control treeView1_ItemDrag event and add the below code.
  1. private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
  2. {
  3. // Move the dragged node when the left mouse button is used.
  4. if (e.Button == MouseButtons.Left)
  5. {
  6. DoDragDrop(e.Item, DragDropEffects.Move);
  7. }
  8. // Copy the dragged node when the right mouse button is used.
  9. else if (e.Button == MouseButtons.Right)
  10. {
  11. DoDragDrop(e.Item, DragDropEffects.Copy);
  12. }
  13. }
After this we will create the treeView1_DragEnter event.
  1. private void treeView1_DragEnter(object sender, DragEventArgs e)
  2. {
  3. e.Effect = e.AllowedEffect;
  4. }
Now we will create the treeView1_DragOver event,
  1. private void treeView1_DragOver(object sender, DragEventArgs e)
  2. {
  3. // Retrieve the client coordinates of the mouse position.
  4. Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
  5. // Select the node at the mouse position.
  6. treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);
  7. }
Now we will create the treeView1_DragDrop event,
  1. private void treeView1_DragDrop(object sender, DragEventArgs e)
  2. {
  3. // Retrieve the client coordinates of the drop location.
  4. Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
  5. // Retrieve the node at the drop location.
  6. TreeNode targetNode = treeView1.GetNodeAt(targetPoint);
  7. // Retrieve the node that was dragged.
  8. TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
  9. // Confirm that the node at the drop location is not
  10. // the dragged node or a descendant of the dragged node.
  11. if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
  12. {
  13. // If it is a move operation, remove the node from its current
  14. // location and add it to the node at the drop location.
  15. if (e.Effect == DragDropEffects.Move)
  16. {
  17. draggedNode.Remove();
  18. targetNode.Nodes.Add(draggedNode);
  19. }
  20. // If it is a copy operation, clone the dragged node
  21. // and add it to the node at the drop location.
  22. else if (e.Effect == DragDropEffects.Copy)
  23. {
  24. targetNode.Nodes.Add((TreeNode)draggedNode.Clone());
  25. }
  26. // Expand the node at the location
  27. // to show the dropped node.
  28. targetNode.Expand();
  29. }
  30. }
After that we will use the below function to perform parent child relation,
  1. private bool ContainsNode(TreeNode node1, TreeNode node2)
  2. {
  3. // Check the parent node of the second node.
  4. if (node2.Parent == null) return false;
  5. if (node2.Parent.Equals(node1)) return true;
  6. // If the parent node is not null or equal to the first node,
  7. // call the ContainsNode method recursively using the parent of
  8. // the second node.
  9. return ContainsNode(node1, node2.Parent);
  10. }
Now we are done. Run the code to check the output.