Microsoft KB Archive/307967

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

Article ID: 307967

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic 2005

-



This article was previously published under Q307967





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



For a Microsoft Visual Basic 6.0 version of this article, see 177743.

IN THIS TASK
SUMMARY
 * Requirements
 * Description of the Technique
 * Steps to Create the Sample
 * Notes

REFERENCES



SUMMARY
This article demonstrates how to perform a drag-and-drop operation with tree nodes between two TreeView controls in a Visual Basic .NET or Visual Basic 2005 application.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual Basic .NET or Visual Basic 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 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 within the bounds of the destination TreeView control. The DragEnter event allows the destination TreeView control to specify whether the drag operation is valid for this control. The code sample in this article allows 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 ensures 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
 Create a new Windows Application in Visual Basic .NET or in Visual Basic 2005. Form1 is created by default. Use the toolbox to add two TreeView controls to Form1. TreeView1 and TreeView2 are created by default. To allow the TreeView controls to be dragged and dropped, change the AllowDrop property of both TreeView1 and TreeView2 to True in the Properties window.  Double-click Form1 to generate the method handler for the Load event of Form1. Add the following code to populate the two TreeView controls with TreeNode objects: Private Sub Form1_Load(ByVal sender As System.Object, _                       ByVal e As System.EventArgs) Handles MyBase.Load Dim ParentNode1 As TreeNode Dim ParentNode2 As TreeNode ParentNode1 = TreeView1.Nodes.Add(&quot;tv1&quot;) With ParentNode1 .Nodes.Add(&quot;tv1FirstChild&quot;) .Nodes.Add(&quot;tv1SecondChild&quot;) .Nodes.Add(&quot;tv1ThirdChild&quot;) .Nodes.Add(&quot;tv1FourthChild&quot;) .Expand End With ParentNode2 = TreeView2.Nodes.Add(&quot;tv2&quot;) With ParentNode2 .Nodes.Add(&quot;tv2FirstChild&quot;) .Nodes.Add(&quot;tv2SecondChild&quot;) .Expand End With End Sub   Add the following method handler to handle the ItemDrag event of either TreeView1 or TreeView2, depending on the direction of the drag operation. This code initiates a move operation on the item that is being dragged. Public Sub TreeView_ItemDrag(ByVal sender As Object, _                                 ByVal e As ItemDragEventArgs) _ Handles TreeView1.ItemDrag, TreeView2.ItemDrag DoDragDrop(e.Item, DragDropEffects.Move) End Sub   Add the following method handler to handle the DragEnter event of TreeView1 or TreeView2, depending on the direction of the drag operation: Public Sub TreeView_DragEnter(ByVal sender As Object, _                              ByVal e As DragEventArgs) _ Handles TreeView2.DragEnter, TreeView1.DragEnter e.Effect = DragDropEffects.Move End Sub </li>  Add the following method to the Form1 class so that it immediately follows the method that you added in the previous step: Public Sub TreeView_DragDrop(ByVal sender As Object, _                             ByVal e As DragEventArgs) _ Handles TreeView2.DragDrop, TreeView1.DragDrop Dim NewNode As TreeNode If e.Data.GetDataPresent(&quot;System.Windows.Forms.TreeNode&quot;, False) Then Dim pt As Point Dim DestinationNode As TreeNode pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y)) DestinationNode = CType(sender, TreeView).GetNodeAt(pt) NewNode = CType(e.Data.GetData(&quot;System.Windows.Forms.TreeNode&quot;), _                                           TreeNode) If Not DestinationNode.TreeView Is NewNode.TreeView Then DestinationNode.Nodes.Add(NewNode.Clone) DestinationNode.Expand 'Remove original node NewNode.Remove End If       End If End Sub </li> Build and run the project. Drag nodes from one TreeView control to the other. Note 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