C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 300,309 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,087 people online right now. Registration is fast and FREE... Join Now!





TreeView Drag n Drop

A bit of code which sets up drag and drop in a treeview. This is handy for if you have a list of items that can be sorted by the user. Here is a bit of code to demonstrate how to do this.

Submitted By: psykoprogrammer
Actions:
Rating:
Views: 23,138

Language: C#

Last Modified: December 4, 2006
Instructions: First create a new Windows executable project. Drop a TreeView component on the form, and let's name it 'myTree' for this demonstration. Go ahead and populate it with some dummy data; enough data to see the dragging and dropping in action. You will need a variable global to the form's scope.

private TreeNode sourceNode;

This is used to track the item we are dragging in our TreeView. Then define the event handlers listed in the code snippet.

Snippet


  1. // The ItemDrag event is called when the item drag begins. Here is
  2. // where you can perform any tracking, or validate if the drag
  3. // operation should occur, and so on.
  4. private void myTree_ItemDrag(object sender, ItemDragEventArgs e)
  5. {
  6.   sourceNode = (TreeNode) e.Item;
  7.   DoDragDrop(e.Item.ToString(), DragDropEffects.Move | DragDropEffects.Copy);
  8. }
  9.  
  10. // Define the event that occurs while the dragging happens
  11. private void myTree_DragEnter(object sender, DragEventArgs e)
  12. {
  13.   if (e.Data.GetDataPresent(DataFormats.Text))
  14.   {
  15.     e.Effect = DragDropEffects.Move;
  16.   else
  17.     e.Effect = DragDropEffects.None;
  18.   }
  19. }
  20.  
  21. // Determine what node in the tree we are dropping on to (target),
  22. // copy the drag source (sourceNode), make the new node and delete
  23. // the old one.
  24. private void myTree_DragDrop(object sender, DragEventArgs e)
  25. {
  26.   Point pos = myTree.PointToClient(new Point(e.X, e.Y));
  27.   TreeNode targetNode = myTree.GetNodeAt(pos);
  28.   TreeNode nodeCopy;
  29.  
  30.   if (targetNode != null)
  31.   {
  32.     nodeCopy = new TreeNode(sourceNode.Text, sourceNode.ImageIndex, sourceNode.SelectedImageIndex);
  33.  
  34.     if (sourceNode.Index > targetNode.Index)
  35.       targetNode.Parent.Nodes.Insert(targetNode.Index, nodeCopy);
  36.     else
  37.       targetNode.Parent.Nodes.Insert(targetNode.Index + 1, nodeCopy);
  38.  
  39.     sourceNode.Remove();
  40.     myTree.Invalidate();
  41.   }
  42. }

Copy & Paste


Comments


johnbillsmith 2009-03-30 23:53:46

I cannot follow the if(... code in my_DragEnter(... if (e.Data.GetDataPresent(DataFormats.Text)) { e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; }

JohnStodden 2009-09-17 04:58:09

1. You omit an instruction to the user to make sure the treeview property AllowDrop is set to true. Otherwise none of the above works! 2. The DragEnter event is only applicable if you are dragging from one treeview to another. In this example, you should use the DragOver event. 3. There is a coding error in the DragEnter event (remove the first curly bracket.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month