home screen

Search



Number Of Result : 0

Result :


Friday, October 31, 2008

TreeView Drap and Drop


  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. }




track URL : clickhere

No comments: