vb.net controls:
I'm implementing a drag-n-drop operation from a TreeView to a ListView
control
Everything seems to work fine except one little hitch...if the user clicks
on and starts dragging (i.e. no MouseUp) a node which was not previously the
SelectedNode, the SelectedNode does not change to the node the user clicked
and dragged on. If the user clicks and releases on the node and then clicks
again to start the drag, it works properly...but obviously less intuitive
for the user.
I have tried setting the SelectedNode in the MouseDown event, which made for
even stranger behavior. I also tried setting the node to drag based on the
item at the center of the _dragRect created in the MouseDown event. Any
ideas on how to make it so if the user clicks on a non-selected node and
immediately drags, the TreeView properly sets the SelectedNode to the node
the user clicked on?
I've attached my MouseDown/MouseUp/MouseMove event handlers below.
Thanks
Dave Taylor
Private Sub tvItems_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvItems.MouseDown
Dim dragSize As Size = SystemInformation.DragSize
Dim pt As Point = tvItems.PointToClient(New Point(e.X, e.Y))
If Not IsNothing(tvItems.GetNodeAt(pt)) Then
_dragRect = New Rectangle(New Point(e.X - (dragSize.Width / 2),
e.Y - (dragSize.Height / 2)), dragSize)
Else
_dragRect = Rectangle.Empty
End If
End Sub
Private Sub tvItems_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvItems.MouseMove
If (e.Button = MouseButtons.Left) Then
If Not _dragRect.Contains(e.X, e.Y) Then
Dim hi As HistorianItem = CType(tvItems.SelectedNode.Tag,
HistorianItem)
If (hi Is Nothing) Then Exit Sub
If hi.HistorianAttribute Is Nothing Then Exit Sub
tvItems.DoDragDrop(hi, DragDropEffects.Link Or
DragDropEffects.Copy)
End If
End If
End Sub
Private Sub tvItems_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvItems.MouseUp
_dragRect = Rectangle.Empty
End Sub