Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView/ Drag & Drop

Status
Not open for further replies.

TomZC

Programmer
Oct 22, 2002
8
0
0
US
I've set up a TreeView (named tvwStuff) in one of my applications and have gotten almost everything working right. The one thing that's not quite the way I want is the Drag and Drop feature. Dragging and dropping a node works, but the dropped node is always made the first sibling of all siblings under the new parent. I want it to always be the last sibling. I've tried to use the LastSibling property in many ways to do what I want, but I'm just not getting it.

Here's my code from frm the dragdrop event. The place where I have four question marks is where I've been attempting to make the dragged node the last sibling if it is not the only child. (I've also put a working code sample in a file named "tree.zip" at in case that makes it easier to check this out.)
Code:
Private Sub tvwStuff_DragDrop(Source As Control, x As Single, y As
Single)
    Dim objParent As Node '(objDragNode is form-level)

' Intercept and exit if dropped on invalid area
If tvwStuff.DropHighlight Is Nothing Then
    blnInDrag = False
    Exit Sub
Else
    ' set parent
    On Error GoTo TrapError
    If tvwStuff.DropHighlight.Children = 0 Then
        Set objDragNode.Parent = tvwStuff.DropHighlight
    Else
        Set objParent = tvwStuff.DropHighlight
        Set objDragNode.Parent = objParent
        '???? Now somehow we need to make objDragNode
        '     the last sibling under objParent
    End If
    Cls
    ' clear out objects
    Set tvwStuff.DropHighlight = Nothing
    blnInDrag = False
    Set objDragNode = Nothing
    Set objParent = Nothing
    Exit Sub ' Exit if no errors occured.
End If

TrapError:
        strGirl = tvwStuff.DropHighlight

        ' Preview image here since click event didn't go
            
            blnInDrag = False
            Set tvwStuff.DropHighlight = Nothing
            Exit Sub
End Sub

other code ...
my form-level variables:
Code:
Private nodBranches As Node
Private blnInDrag As Boolean            ' for TreeView's drag & drop
Private objDragNode As Object           ' Node being dragged

Dragover, mousedown, and mousemove events:
Code:
Private Sub tvwStuff_DragOver(Source As Control, _
                              x As Single, y As Single, _
                              State As Integer)
    If blnInDrag = True Then
        ' Set DropHighlight to the mouse's coordinates.
        Set tvwStuff.DropHighlight = tvwStuff.HitTest(x, y)
    End If
End Sub

Private Sub tvwStuff_MouseDown(Button As Integer, Shift As Integer, x
As Single, y As Single)
    Set tvwStuff.DropHighlight = tvwStuff.HitTest(x, y)

    If Not tvwStuff.DropHighlight Is Nothing Then
        tvwStuff.SelectedItem = tvwStuff.HitTest(x, y)
        Set objDragNode = tvwStuff.SelectedItem ' Set the item being
dragged.
    End If
    Set tvwStuff.DropHighlight = Nothing
End Sub

Private Sub tvwStuff_MouseMove(Button As Integer, Shift As Integer, x
As Single, y As Single)

  If Button = vbLeftButton Then ' dragging
        blnInDrag = True
        tvwStuff.Drag vbBeginDrag ' Drag operation.
    End If
End Sub
 
Hello again. Disregard the above post. I've found a solution.

On the other hand if someone would like to know what ended up working, then post a reply and I'll post the code.
 
I found a solution at Turns out it works fine if there are a relatively small number of siblings, but it can be unwieldy for large numbers of siblings. For this reason, I'm limiting this sub's use to 15 or less siblings.

Here's the code:

Under the "tvwStuff.DropHighlight Is Nothing" If/Then/Else statement under "Else" in tvwStuff_DragDrop, I the code to this:

Code:
Set objParent = tvwStuff.DropHighlight
Set objDragNode.Parent = objParent
If objParent.Children < 15 Then
Call SortOneNode(objDragNode)   ' make dragged node last sibling
End If

The sub called looks like this:
Code:
Private Sub SortOneNode(ByVal MovedNode As Node)
       Dim NextNode As Node

   'start looping through the next nodes...
   Do Until (MovedNode.Next Is Nothing)

       'get the next node
       Set NextNode = MovedNode.Next
       
       'here you can compare the nodes to determine
       'if you should move it down further
       'using (True) will move the node to the bottom.
       If (True) Then
           'move the node one step down
           Set NextNode.Parent = MovedNode.Parent
           Set NextNode = MovedNode.Next
       Else
           'stop moving
           Exit Do
       End If

   Loop
   'clear the objects
   Set NextNode = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top