Microsoft KB Archive/815675

= How to add TreeView drag-and-drop functionality in a Visual C++ .NET or Visual C++ 2005 application =

Article ID: 815675

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 307968.



For a Microsoft Visual Basic .NET version of this article, see 307967.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Description of the technique
 * Steps to create the sample
 * Notes
 * REFERENCES



SUMMARY
This step-by-step article describes how to perform a drag-and-drop operation with tree nodes between two TreeView controls in a Visual C++ .NET or Visual C++ 2005 application.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C++ .NET or Microsoft Visual C++ 2005

This article assumes that you are familiar with the following topics:
 * Windows Forms TreeView control
 * Windows Forms event handling

back to the top

Description of the technique
The TreeView control provides three drag-and-drop events that you must handle:
 * ItemDrag: This event is raised from the source TreeView control as soon as the user starts to drag the tree node. When this behavior occurs, call the DoDragDrop method to initiate the drag-and-drop procedure.
 * DragEnter: After you initiate the drag-and-drop operation, you must handle the DragEnter event in the destination TreeView control. This event occurs when the user drags the TreeNode object from the source TreeView control to a point in the bounds of the destination TreeView control. The DragEnter event permits the destination TreeView control to specify whether the drag operation is valid for this control. The code sample in this article permits only the move operation.
 * DragDrop: The last event to handle is the DragDrop event of the destination TreeView control. This event occurs when the TreeNode object that is dragged has been dropped on the destination TreeView control. To handle this event, retrieve the TreeNode object, and add the object to the destination TreeView control. The code sample uses the Data object to retrieve the data.

The code sample in this article makes sure that a TreeNode object has been dragged to the destination TreeView control. The GetData method of the Data object retrieves the node that is dragged from the source control. The GetNodeAt method determines where this node is dropped on the destination control. After you determine the position, add the source node as a child of the destination node. Because this sample performs a move operation, the last step is to remove the source node from the original TreeView control.

back to the top

Steps to create the sample
 Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates. By default, Form1 is created.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates. Add two TreeView controls to Form1. By default, TreeView1 and TreeView2 are created. Change the AllowDrop property of both TreeView1 and TreeView2 to True.  Double-click Form1 to generate the method handler for the Load event of Form1. To populate the two TreeView controls with TreeNode objects and to define the event handlers, add the following code. private: System::Void Form1_Load(System::Object * sender, System::EventArgs *  e) { TreeNode* ParentNode1; TreeNode* ParentNode2;

ParentNode1 = treeView1->Nodes->Add(&quot;tv1&quot;); ParentNode1->Nodes->Add(&quot;tv1FirstChild&quot;); ParentNode1->Nodes->Add(&quot;tv1SecondChild&quot;); ParentNode1->Nodes->Add(&quot;tv1ThirdChild&quot;); ParentNode1->Nodes->Add(&quot;tv1FourthChild&quot;); ParentNode1->Expand;

ParentNode2 = treeView2->Nodes->Add(&quot;tv2&quot;); ParentNode2->Nodes->Add(&quot;tv2FirstChild&quot;); ParentNode2->Nodes->Add(&quot;tv2SecondChild&quot;); ParentNode2->Expand; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article. </li>  Paste the following ItemDrag event handler after the Form1_Load method. This code initiates a move operation on the item that is being dragged. This event handler is shared by TreeView1 and TreeView2. private: void treeView_ItemDrag(System::Object * sender, System::Windows::Forms::ItemDragEventArgs *  e) { DoDragDrop(e->Item, DragDropEffects::Move); } </li>  Paste the following DragEnter event handler immediately after the method that you added in the previous step. private: void treeView_DragEnter(System::Object * sender, System::Windows::Forms::DragEventArgs* e) { e->Effect = DragDropEffects::Move; } </li>  Paste the following DragDrop event handler immediately after the method that you added in the previous step. private: void treeView_DragDrop(System::Object * sender, System::Windows::Forms::DragEventArgs* e) { TreeNode* NewNode; if(e->Data->GetDataPresent(&quot;System.Windows.Forms.TreeNode&quot;, false)) {        Point p(e->X,e->Y); Point pt = __try_cast<TreeView*>(sender)->PointToClient(p); TreeNode * DestinationNode = __try_cast<TreeView *>(sender)->GetNodeAt(pt); NewNode = __try_cast<TreeNode *>(e->Data->GetData(&quot;System.Windows.Forms.TreeNode&quot;)); if(DestinationNode->TreeView != NewNode->TreeView) {                 DestinationNode->Nodes->Add(__try_cast<TreeNode*>(NewNode->Clone)); DestinationNode->Expand; //Remove Original Node NewNode->Remove; }    } } </li> Build and run the project. Drag nodes from either TreeView control to the other. Notice that the node is removed from the source control and is added as a child node in the destination control.</li></ol>

back to the top