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.)
other code ...
my form-level variables:
Dragover, mousedown, and mousemove events:
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