Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user drag items from one position to another in a TreeView
KeywordsTreeView, drag and drop
CategoriesControls
 
In the TreeView's MouseDown event, save the node the mouse is over. In the MouseMove event handler, see what kind of node this is. Set the TreeView's DragIcon property to an icon that is appropriate for this node type and start the drag.
 
' Save the node pressed so we can drag it later.
Private Sub OrgTree_MouseDown(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    Set SourceNode = OrgTree.HitTest(x, y)
End Sub

' Start a drag if one is not in progress.
Private Sub OrgTree_MouseMove(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    If SourceNode Is Nothing Then Exit Sub

    If Button = vbLeftButton Then
        ' Start a new drag. Note that we do not get
        ' other MouseMove events while the drag is
        ' in progress.
        
        ' See what node we are dragging.
        SourceType = NodeType(SourceNode)

        ' Select this node. When no node is highlighted,
        ' this node will be displayed as selected. That
        ' shows where it will land if dropped.
        Set OrgTree.SelectedItem = SourceNode

        ' Set the drag icon for this source.
        OrgTree.DragIcon = IconImage(SourceType)
        OrgTree.Drag vbBeginDrag
    End If
End Sub
 
In the TreeView's DragOver event handler, use the HitTest method to see which node is under the mouse. If the node we are over is the appropriate type for the node we are dragging, highlight it by setting the control's DropHighlight property to the node. If it is not the right type of node, set DropHighlight to Nothing.
 
' The mouse is being dragged over the control.
' Highlight the appropriate node.
Private Sub OrgTree_DragOver(Source As Control, x As _
    Single, y As Single, State As Integer)
Dim target As Node
Dim highlight As Boolean

    If SourceNode Is Nothing Then Exit Sub

    ' See what node we're above.
    Set target = OrgTree.HitTest(x, y)

    ' If it's the same as last time, do nothing.
    If target Is TargetNode Then Exit Sub
    Set TargetNode = target

    highlight = False
    If Not (TargetNode Is Nothing) Then
        ' See what kind of node were above.
        If NodeType(TargetNode) + 1 = SourceType Then _
            highlight = True
    End If

    If highlight Then
        Set OrgTree.DropHighlight = TargetNode
    Else
        Set OrgTree.DropHighlight = Nothing
    End If
End Sub
 
In the TreeView's DragDrop event, see if the DropHighlight property refers to a node. If so, move the source node so it is a child of this node.
 
' The user is dropping. See if the drop is valid.
Private Sub OrgTree_DragDrop(Source As Control, x As _
    Single, y As Single)
    If SourceNode Is Nothing Then Exit Sub

    If Not (OrgTree.DropHighlight Is Nothing) Then
        ' It's a valid drop. Set source node's
        ' parent to be the target node.
        Set SourceNode.Parent = OrgTree.DropHighlight
        Set OrgTree.DropHighlight = Nothing
    End If

    Set SourceNode = Nothing
    SourceType = otNone
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated