Option Explicit
Private Sub Form_Load()
Dim objNode As Node
Treeview1.DragMode = vbManual
Set objNode = TreeView1.Nodes.Add(, , "D", "Mike")
objNode.Expanded = True
Set objNode = TreeView1.Nodes.Add(, , "A", "Alice")
objNode.Expanded = True
Set objNode = TreeView1.Nodes.Add(, , "M", "Carol")
objNode.Expanded = True
Call TreeView1.Nodes.Add("D", tvwChild, "C1", "Greg")
Call TreeView1.Nodes.Add("D", tvwChild, "C2", "Peter")
Call TreeView1.Nodes.Add("D", tvwChild, "C3", "Bobby")
Call TreeView1.Nodes.Add("M", tvwChild, "C4", "Marcia")
Call TreeView1.Nodes.Add("M", tvwChild, "C5", "Jan")
Call TreeView1.Nodes.Add("M", tvwChild, "C6", "Cindy")
End Sub
Private Sub Treeview1_DragDrop(Source As Control, x As Single, y As Single)
Dim objNode As Node
If Source Is TreeView1 Then
'Make sure there is a Node to at the location
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
If Not TreeView1.DropHighlight Is Nothing Then
'Make sure the Source Node is not the Parent of the Target Node and
'the Source Node is not the Target Node
If Not TreeView1.DropHighlight.Parent Is TreeView1.SelectedItem And _
Not TreeView1.DropHighlight Is TreeView1.SelectedItem Then
'Moves the Source Node under the Target Node
Set TreeView1.SelectedItem.Parent = TreeView1.DropHighlight
End If
End If
'Clean up the Drag operation
Set TreeView1.DropHighlight = Nothing
TreeView1.Drag vbEndDrag
End If
End Sub
Private Sub Treeview1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
'Will show which node will be used as the Target
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End Sub
Private Sub Treeview1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim objNode As Node
'Start a Drag operation on a Right Mouse click
If Button = vbRightButton Then
'Make sure the Source Node of the Drag operation exists and is selected.
Set objNode = TreeView1.HitTest(x, y)
If Not objNode Is Nothing Then
objNode.Selected = True
TreeView1.Drag vbBeginDrag
End If
End If
End Sub