Treeview Control (Reacting to Drag and Drop Event):   Return to List

Private Sub Treeview1_DragDrop(Source As Control, x As Single, y As Single)
If optDragOn.Value = True Then
    If Treeview1.DropHighlight Is Nothing Then
        Set Treeview1.DropHighlight = Nothing
        inDrag = False
        Exit Sub
    Else
        strTempKey = Treeview1.DropHighlight.Key
        ' Key must demonstrate level 2
        If Left(strTempKey, 1) <> "2" Then
            MsgBox "Can only drop on Level 2!", vbOKOnly, "Drop Positioning Error"
            Exit Sub' We don't want to drop on another level 3 or on a level 1
        End If
        If xNod = Treeview1.DropHighlight Then
            MsgBox "Cannot drop on self!", vbOKOnly, "Drop Position Error"
            Exit Sub' We don't want to drop over same item
        End If
        ' New Dad Text and GrandPa Text for Record change
        strDadText = Treeview1.DropHighlight.Text
        strGrandPaText = Treeview1.DropHighlight.Parent.Text
        strNodeKey = xNod.Key
        strNodeText = xNod.Text
        intOriginalParentIndex = xNod.Parent.Index
        intOriginalGrandparentIndex = xNod.Parent.Parent.Index
        intAuntsAndUncles = xNod.Parent.Parent.Children
        intSiblings = xNod.Parent.Children - 1
        If Treeview1.selecteditem.Index <> 1 Then
            Treeview1.Nodes.Remove (Treeview1.selecteditem.Index)
            If intSiblings = 0 Then' If no other children then remove the parent node
                Treeview1.Nodes.Remove (intOriginalParentIndex)
                If intAuntsAndUncles = 1 Then' Dad's gone -- now SHOOT GRANDMA!
                    Treeview1.Nodes.Remove (intOriginalGrandparentIndex)
                End If
            End If
            Treeview1.Nodes.Add strTempKey, tvwChild, strNodeKey, strNodeText
            EditTheRecords strDadText, strGrandPaText, strNodeKey
        End If
        Set Treeview1.DropHighlight = Nothing
        inDrag = False
    End If
End If
End Sub

' All nodes in a Treeview have 2 basic properties:
    Node.Text -- The text displayed in the Treeview control
    Node.Key -- The assigned key -- used to attach children & perform other manipulations on the particular node.

See also:
Treeview ... Adding Nodes
Treeview ... Adding Root Node
Treeview ... Collapsing Nodes to Root Level
Treeview ... Complete Subprocedure for Collapsing Nodes
Treeview ... Counting Number of Nodes
Treeview ... Expanding All Nodes
Treeview ... Determing selected Node and Highlighting it
Treeview ... ChildNode Info of Selected Node
Treeview ... Text of Selected Node
Treeview ... Node Click Event
Treeview ... Number of Children (in parens) appended to Text of Selected Node
Treeview ... Reacting to DragOver in a Treeview
Treeview ... Repositioning Cursor to the Top of the Treeview



Note to Webmaster