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

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

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.

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

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.

Steps to Create the Sample

  1. Create a new Windows Application in Visual Basic .NET or in Visual Basic 2005. Form1 is created by default.
  2. Use the toolbox to add two TreeView controls to Form1. TreeView1 and TreeView2 are created by default.
  3. 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.
  4. 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("tv1")
    With ParentNode1
    .Nodes.Add("tv1FirstChild")
    .Nodes.Add("tv1SecondChild")
    .Nodes.Add("tv1ThirdChild")
    .Nodes.Add("tv1FourthChild")
    .Expand()
    End With
    ParentNode2 = TreeView2.Nodes.Add("tv2")
    With ParentNode2
    .Nodes.Add("tv2FirstChild")
    .Nodes.Add("tv2SecondChild")
    .Expand()
    End With
    End Sub
  5. 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
  6. 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
  7. 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("System.Windows.Forms.TreeNode", 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("System.Windows.Forms.TreeNode"), _
    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
  8. 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.

Notes

The sample that is provided in this article is intended for demonstration purposes only; as such, the sample only illustrates how to use the TreeView control in a simplified scenario. To keep the sample small, several scenarios are not taken into consideration. For example, the code does not allow you to perform a drag-and-drop operation with nodes in the same TreeView control.

The code also does not handle the scenario in which a user does not drop the object on a particular node in the destination TreeView control. To handle this scenario, check if the DestinationNode is null; if it is, you can add the object that is being dragged to the root of the TreeView control, and so on.

References

For more information, refer to the following topics in the .NET Framework Software Development Kit (SDK) documentation:
Свойства

Номер статьи: 307967 — последний просмотр: 13 мая 2007 г. — редакция: 1

Отзывы и предложения