Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How To Perform Drag-Drop Operations on TreeView Control in Windows Forms

, 9 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Friends, Drag/Drop functionality is a ease to end user in each and every technology and almost every user is comfortable using the drag-drop feature. In this post we will see how to perform drag and drop operation of nodes in a single TreeView control. There are few events that we will be working wi

Friends,

Drag/Drop functionality is a ease to end user in each and every technology and almost every user is comfortable using the drag-drop feature. In this post we will see how to perform drag and drop operation of nodes in a single TreeView control. There are few events that we will be working with. They are – ItemDrag(), DragEnter() and DragDrop(). Apart from these, we will also use some inbuilt functions that facilitate drag/drop operations on Windows forms. So, Let’s get started. To create a draggable/drop-able  Treeview control on windows forms, we will use the steps mentioned below -

  • Set the AllowDrop property of TreeView control to True.
  • Populate the TreeView with some nodes. I am here filling the nodes with some sample data.
         private function LoadData()
         {
                for (int i = 0; i < 5; i++)
                {
                    TreeNode node = new TreeNode("Node " + i);
                    for (int j = 0; j < 3; j++)
                    {
                        TreeNode n1 = new TreeNode("Sub-node :" + i + " - " + j);
                        node.Nodes.Add(n1);
                    }
                    treeView1.Nodes.Add(node);
                }
         }
    
  • Handle the ItemDrag() event handler of the TreeView and write the code below-
         private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
         {
               DoDragDrop(e.Item, DragDropEffects.Move);
         }
    
  • Handle the DragEnter() event handler of the TreeView and write the code below-
         private void treeView1_DragEnter(object sender, ItemDragEventArgs e)
         {
               e.Effect = DragDropEffects.Move;
         }
    
  • Handle the DragDrop() event handler of the TreeView and write the code below-
         private void treeView1_DragDrop(object sender, ItemDragEventArgs 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");
                    DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
                    DestinationNode.Expand();
                    //Remove Original Node
                    NewNode.Remove();
                }
         }
    
  • You're done.
  • Execute the application and try dragging/dropping the nodes on the TreeView control.

Keep learning and sharing! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.
 
I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 10 Jun 2014
Article Copyright 2014 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid