Click here to Skip to main content
15,868,419 members
Articles / Desktop Programming / MFC
Article

Drag & Drop in Tree view

Rate me:
Please Sign up or sign in to vote.
2.86/5 (51 votes)
3 Jun 20031 min read 196.8K   4.5K   52   19
This is basically an article which demonstrates the Drag & Drop operation in a TreeView.

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:

C#
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.
C#
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


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
grciattsbcbs23-Jun-17 1:11
grciattsbcbs23-Jun-17 1:11 
GeneralMy vote of 1 Pin
Prasyee13-Sep-12 1:04
Prasyee13-Sep-12 1:04 
GeneralMy vote of 3 Pin
Member 856357826-Aug-12 15:59
Member 856357826-Aug-12 15:59 
GeneralGood to start understanding how it works but not optimized and buggy... Pin
krypto_46730-Mar-12 4:49
krypto_46730-Mar-12 4:49 
GeneralDrag & Drop treeview nodes Pin
qzeb15-Sep-06 2:14
qzeb15-Sep-06 2:14 
GeneralGreat article, but not the same in Managed C++ Pin
18-Oct-05 8:04
suss18-Oct-05 8:04 
Generalanother small bug Pin
xilefxilef29-Aug-05 9:08
xilefxilef29-Aug-05 9:08 
GeneralBug in the code Pin
Emiliano9-Jun-05 13:17
Emiliano9-Jun-05 13:17 
GeneralAnother small extension: drag all nodes Pin
amlinger28-Dec-04 22:23
amlinger28-Dec-04 22:23 
Generalsmall extension Pin
Anonymous22-Apr-04 0:27
Anonymous22-Apr-04 0:27 
GeneralNot necessary to store the node-to-delete Pin
pplppp3-Feb-04 13:33
pplppp3-Feb-04 13:33 
GeneralNeed drag & drop examples using the CTrl button Pin
Saradhi17-Dec-03 20:31
Saradhi17-Dec-03 20:31 
GeneralRe: Need drag & drop examples using the CTrl button Pin
pplppp3-Feb-04 13:30
pplppp3-Feb-04 13:30 
GeneralOn drop scrolling Pin
Garmeister23-Sep-03 11:51
Garmeister23-Sep-03 11:51 
GeneralNot Completely Obvious Pin
JohnAndre25-Aug-03 6:44
JohnAndre25-Aug-03 6:44 
GeneralRe: Not Completely Obvious Pin
Saradhi17-Dec-03 20:30
Saradhi17-Dec-03 20:30 
Generalyay. Pin
langdonx20-Jul-03 5:51
langdonx20-Jul-03 5:51 
QuestionUmmm am I missing something? Pin
Ray Cassick5-Jun-03 12:38
Ray Cassick5-Jun-03 12:38 
AnswerRe: Ummm am I missing something? Pin
ItsAWonderfulLife5-Jun-03 18:13
ItsAWonderfulLife5-Jun-03 18:13 

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

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