- Visual Basic .NET or Visual Basic 2005
- Windows Forms TreeView control
- Windows Forms event handling
- 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.
- 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("tv1")
ParentNode2 = TreeView2.Nodes.Add("tv2")
- 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
- 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
- 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"), _
If Not DestinationNode.TreeView Is NewNode.TreeView Then
'Remove original node
- 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.
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.
ID do Artigo: 307967 - Última Revisão: 13 de mai de 2007 - Revisão: 1