Drop items on specific location in listview
-
Hi, I've been messing with this for a while and am obviously missing something but i just don't see it :s... I'm trying to do a drag drop between 2 listviews and want to drop on a specific location.. Although i think my code is right (which it obviously isn't :p) .. it always returns index -1 on the droplocation.. my code:
Private Sub LVPlaylist_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LVPlaylist.DragOver
' Retrieve the client coordinates of the mouse pointer.
Dim targetPoint As Point = LVPlaylist.PointToClient(New Point(e.X, e.Y))' Retrieve the index of the item closest to the mouse pointer. Dim targetIndex As Integer = \_ LVPlaylist.InsertionMark.NearestIndex(targetPoint) 'just some visual feedback to see where I am dragging.. -1 always anyways.. LblTargetIndex.Text = LVPlaylist.GetItemAt(targetPoint.X, targetPoint.Y).ToString ' Confirm that the mouse pointer is not over the dragged item. If targetIndex > -1 Then ' Determine whether the mouse pointer is to the left or ' the right of the midpoint of the closest item and set ' the InsertionMark.AppearsAfterItem property accordingly. Dim itemBounds As Rectangle = LVPlaylist.GetItemRect(targetIndex) If targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) Then LVPlaylist.InsertionMark.AppearsAfterItem = True Else LVPlaylist.InsertionMark.AppearsAfterItem = False End If End If ' Set the location of the insertion mark. If the mouse is ' over the dragged item, the targetIndex value is -1 and ' the insertion mark disappears. LVPlaylist.InsertionMark.Index = targetIndex End Sub
Private Sub LVPlaylist_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles LVPlaylist.DragDrop
Dim MyItem As ListViewItem
Dim MyItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListviewItem()")
Dim i As Integer = 0
Dim targetIndex As Integer = LVPlaylist.InsertionMark.Index
For Each MyItem In LVfiles.SelectedItems
'is -1 ALWAYS so put it at the top...
If targetindex = -1 Then targetindex = 0
'TODO: make sure the targetindex get's ++ for every item dropped
Dim newitem As ListViewItem = MyItem.Clone
LVPlaylist.Items.Insert