Public Class Form1
Public Sub TreeView1_ItemDrag(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ItemDragEventArgs) _
Handles TreeView1.ItemDrag
'Set the drag node and initiate the DragDrop
DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
Public Sub TreeView1_DragEnter(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles TreeView1.DragEnter
'See if there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
True) Then
'TreeNode found allow move effect
e.Effect = DragDropEffects.Move
Else
'No TreeNode found, prevent move
e.Effect = DragDropEffects.None
End If
End Sub
Public Sub TreeView1_DragOver(ByVal sender As System.Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) = False Then Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreeview As TreeView = CType(sender, TreeView)
'As the mouse moves over nodes, provide feedback to
'the user by highlighting the node that is the
'current drop target
Dim pt As Point = _
CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
Dim targetNode As TreeNode = selectedTreeview.GetNodeAt(pt)
'See if the targetNode is currently selected,
'if so no need to validate again
If Not (selectedTreeview.SelectedNode Is targetNode) Then
'Select the node currently under the cursor
selectedTreeview.SelectedNode = targetNode
'Check that the selected node is not the dropNode and
'also that it is not a child of the dropNode and
'therefore an invalid target
Dim dropNode As TreeNode = _
CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
TreeNode)
Do Until targetNode Is Nothing
If targetNode Is dropNode Then
e.Effect = DragDropEffects.None
Exit Sub
End If
targetNode = targetNode.Parent
Loop
End If
'Currently selected node is a suitable target
e.Effect = DragDropEffects.Move
End Sub
Public Sub TreeView1_DragDrop(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles TreeView1.DragDrop
'Check that there is a TreeNode being dragged
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
True) = False Then Exit Sub
'Get the TreeView raising the event (incase multiple on form)
Dim selectedTreeview As TreeView = CType(sender, TreeView)
'Get the TreeNode being dragged
Dim dropNode As TreeNode = _
CType(e.Data.GetData("System.Windows.Forms.TreeNode"), _
TreeNode)
'The target node should be selected from the DragOver event
Dim targetNode As TreeNode = selectedTreeview.SelectedNode
'Remove the drop node from its current location
dropNode.Remove()
'If there is no targetNode add dropNode to the bottom of
'the TreeView root nodes, otherwise add it to the end of
'the dropNode child nodes
If targetNode Is Nothing Then
selectedTreeview.Nodes.Add(dropNode)
Else
targetNode.Nodes.Add(dropNode)
End If
'Ensure the newley created node is visible to
'the user and select it
dropNode.EnsureVisible()
selectedTreeview.SelectedNode = dropNode
End Sub
End Class