Click here to Skip to main content
Licence 
First Posted 3 Jun 2003
Views 128,712
Downloads 830
Bookmarked 46 times

Drag & Drop in Tree view

By ItsAWonderfulLife | 3 Jun 2003
This is basically an article which demonstrates the Drag & Drop operation in a TreeView.
14 votes, 31.1%
1
9 votes, 20.0%
2
2 votes, 4.4%
3
6 votes, 13.3%
4
14 votes, 31.1%
5
2.79/5 - 45 votes
μ 2.63, σa 2.97 [?]

Sample Image - TreeDragnDrop.jpg

Introduction

I needed drag 'n' drop facility in TreeView control, which I guess is quite a common functionality. But I couldn't find it, so I implemented it.

Thinking it might be useful to other people, I am sharing the code here.

Implementation

In this app, I have provided the functionality to add children or siblings to a node via toolbar.

Events that are required to be implemented are:

  1. ItemDrag
  2. DragDrop
  3. DragEnter

of the TreeView control.

In DragItem event, simply store the selected node that is the node to be dragged, in a TreeNode type class variable & call DoDragDrop as shown below:

NodeToBeDeleted = (TreeNode)e.Item;
   string strItem = e.Item.ToString();
   DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);

In DragEnter, set appropriate effects.

In DragDrop, do the following steps:

  1. Get the current mouse position, that is coordinates where to drop the dragged node.
  2. Use PointToClient method of the TreeView control to get the coordinates relative to the TreeView control.

  3. Get the node at this position (that is the node on which to drop the dragged node)
  4. Then delete the node dragged from its current position & insert it at the next index of the node at which to drop. That's it.
Position.X = e.X;
   Position.Y = e.Y;
   Position = treeView1.PointToClient(Position);
   TreeNode DropNode = this.treeView1.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);
   }

Now the code is ready to be executed

Conclusion

I hope it helps u ppl. All the best.

Do give feedback on this article as positive criticism is always healthy for growth.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ItsAWonderfulLife

Web Developer

Sweden Sweden

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDrag & Drop treeview nodes Pinmemberqzeb3:14 15 Sep '06  
GeneralGreat article, but not the same in Managed C++ PinmemberScott mLeffler9:04 18 Oct '05  
Generalanother small bug Pinmemberfelimonchik10:08 29 Aug '05  
GeneralBug in the code PinmemberEmiliano14:17 9 Jun '05  
GeneralAnother small extension: drag all nodes Pinmemberamlinger23:23 28 Dec '04  
Generalsmall extension PinsussAnonymous1:27 22 Apr '04  
GeneralNot necessary to store the node-to-delete Pinmemberpplppp14:33 3 Feb '04  
GeneralNeed drag & drop examples using the CTrl button PinmemberSaradhi21:31 17 Dec '03  
GeneralRe: Need drag & drop examples using the CTrl button Pinmemberpplppp14:30 3 Feb '04  
GeneralOn drop scrolling PinmemberGarmeister12:51 23 Sep '03  
GeneralNot Completely Obvious PinmemberJohnAndre7:44 25 Aug '03  
GeneralRe: Not Completely Obvious PinmemberSaradhi21:30 17 Dec '03  
Generalyay. Pinmemberlangdonx6:51 20 Jul '03  
QuestionUmmm am I missing something? PinmemberRay Cassick13:38 5 Jun '03  
AnswerRe: Ummm am I missing something? PinmemberItsAWonderfulLife19:13 5 Jun '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 4 Jun 2003
Article Copyright 2003 by ItsAWonderfulLife
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid