Tag Archives: TreeView control

Drag and drop a tree node from one node another node in c#

This article demonstrate how we can add drag and drop functionality in one Treeview or between two or more Treeview controls. The following example shows how we can how we can drag and drop one treenode to other node of the treeview control.

below example contains two treeview named treeView1 and treeView2. you need to set the AllowDrop property of both TreeView1 and TreeView2 to True in the Properties window. In this example we are using common drag and drop events.

      
  public Form1()
        {
            InitializeComponent();

            TreeNode ParentNode1;
            TreeNode ParentNode2;

            TreeNode ParentNode3;

            ParentNode1 = treeView1.Nodes.Add("Fruits");
            ParentNode1.Nodes.Add("Apple");
            ParentNode1.Nodes.Add("Banana");
            ParentNode1.Nodes.Add("Mango");
            ParentNode1.Nodes.Add("Graps");
            ParentNode1.Expand();

            ParentNode3 = treeView1.Nodes.Add("Colors");
            ParentNode3.Nodes.Add("Red");
            ParentNode3.Nodes.Add("Yellow");
            ParentNode3.Nodes.Add("Green");           
            ParentNode3.Expand();

            ParentNode2 = treeView2.Nodes.Add("Store");
            ParentNode2.Nodes.Add("Store1");
            ParentNode2.Nodes.Add("Store2");
            ParentNode2.Expand();

            this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
           this.treeView2.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
            this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
            this.treeView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
            this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
            this.treeView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
        }

        private void treeView_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
        {
            DoDragDrop(e.Item, DragDropEffects.Move);
   }
        private void treeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            TreeNode NewNode;

            if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
            {
                Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
                NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                //if (DestinationNode.TreeView != NewNode.TreeView)
                //{
                    DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
                    DestinationNode.Expand();
                    //Remove Original Node
                    NewNode.Remove();
               // }
            }
        }