none
Moving a TreeNode in a TreeView RRS feed

  • Question

  • Hi,

    Here is my problem:

    How can I move a node in a treeview.

    for example

    Root
    --1
    ----11
    ----12
    ----13
    ----14
    --2
    etc...

    Now I would like to select 14 and move it one up so the result would be:

    Root
    --1
    ----11
    ----12
    ----14
    ----13
    --2
    etc...


    If you know the answer to my problem, please help me?
    Code examples would be very helpful

    thanx
    Friday, June 15, 2007 3:22 PM

Answers

  • Try something like this:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          treeView1.AllowDrop = true;
          this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView1_DragDrop);
          this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
          this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
        }
        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) {
          treeView1.DoDragDrop(e.Item, DragDropEffects.Move);
        }
        private void treeView1_DragOver(object sender, DragEventArgs e) {
          e.Effect = DragDropEffects.None;
          TreeNode srce = (TreeNode)e.Data.GetData(typeof(TreeNode));
          Point loc = treeView1.PointToClient(new Point(e.X, e.Y));
          TreeNode dest = treeView1.GetNodeAt(loc);
          if (dest == null) return;
          for (TreeNode parent = dest.Parent; parent != null; parent = parent.Parent)
            if (parent == srce) return;    // Can't move a parent node up
          dest.Expand();
          e.Effect = DragDropEffects.Move;
        }
        private void treeView1_DragDrop(object sender, DragEventArgs e) {
          Point loc = treeView1.PointToClient(new Point(e.X, e.Y));
          TreeNode dest = treeView1.GetNodeAt(loc);
          TreeNode srce = (TreeNode)e.Data.GetData(typeof(TreeNode));
          if (srce.Parent == null) srce.TreeView.Nodes.Remove(srce);
          else srce.Parent.Nodes.Remove(srce);
          if (dest.Parent == null) dest.TreeView.Nodes.Insert(dest.Index + 1, srce);
          else dest.Parent.Nodes.Insert(dest.Index + 1, srce);
        }
      }

    Friday, June 15, 2007 5:54 PM
    Moderator

All replies

  • In code or with the mouse?
    Friday, June 15, 2007 4:09 PM
    Moderator
  • Mouse, Drag and drop function, I was using this current code, but this only allow me to move the node in same level, and I want to move it freely (exept for root node)

     private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                NodeToBeDeleted = (TreeNode)e.Item;
                string strItem = e.Item.ToString();
                DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);
            }
            private void treeView1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.Text))
                    e.Effect = DragDropEffects.Move;
                else
                    e.Effect = DragDropEffects.None;
            }
            private void treeView1_DragDrop(object sender, DragEventArgs e)
            {
                Position.X = e.X;
                Position.Y = e.Y;
                Position = treeViewArbol.PointToClient(Position);
                TreeNode DropNode = this.treeViewArbol.GetNodeAt(Position);
                if (DropNode != null && DropNode.Parent == this.NodeToBeDeleted.Parent)
                {
                    TreeNode DragNode = this.NodeToBeDeleted;
                    DropNode.Parent.Nodes.Remove(this.NodeToBeDeleted);
                    DropNode.Parent.Nodes.Insert(DropNode.Index + 1, DragNode);
                }
            }
    Friday, June 15, 2007 4:19 PM
  • I half fixxed it, just removed  DropNode.Parent == this.NodeToBeDeleted.Parent...
    But I can still move menu ^^

    Friday, June 15, 2007 4:25 PM
  • Try something like this:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          treeView1.AllowDrop = true;
          this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView1_DragDrop);
          this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
          this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
        }
        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) {
          treeView1.DoDragDrop(e.Item, DragDropEffects.Move);
        }
        private void treeView1_DragOver(object sender, DragEventArgs e) {
          e.Effect = DragDropEffects.None;
          TreeNode srce = (TreeNode)e.Data.GetData(typeof(TreeNode));
          Point loc = treeView1.PointToClient(new Point(e.X, e.Y));
          TreeNode dest = treeView1.GetNodeAt(loc);
          if (dest == null) return;
          for (TreeNode parent = dest.Parent; parent != null; parent = parent.Parent)
            if (parent == srce) return;    // Can't move a parent node up
          dest.Expand();
          e.Effect = DragDropEffects.Move;
        }
        private void treeView1_DragDrop(object sender, DragEventArgs e) {
          Point loc = treeView1.PointToClient(new Point(e.X, e.Y));
          TreeNode dest = treeView1.GetNodeAt(loc);
          TreeNode srce = (TreeNode)e.Data.GetData(typeof(TreeNode));
          if (srce.Parent == null) srce.TreeView.Nodes.Remove(srce);
          else srce.Parent.Nodes.Remove(srce);
          if (dest.Parent == null) dest.TreeView.Nodes.Insert(dest.Index + 1, srce);
          else dest.Parent.Nodes.Insert(dest.Index + 1, srce);
        }
      }

    Friday, June 15, 2007 5:54 PM
    Moderator