Dim iSelectedIndex As Integer
Dim iLastItemIndex As Integer
Private Sub Listview1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragDrop
'get the text from our dropped item and assign it to the hovered item's text property
Dim sData As String = e.Data.GetData(DataFormats.Text).ToString()
Listview1.Items(iLastItemIndex).Text = sData
End Sub
Private Sub Listbox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Listbox1.MouseMove
'if holding down left mouse, start our dragdrop by passing the selected index from the listbox
If e.Button = MouseButtons.Left Then
Listbox1.DoDragDrop(Listbox1.Items(iSelectedIndex), DragDropEffects.Copy)
End If
End Sub
Private Sub Listview1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragEnter
'set the drag effect based on our drag data
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Listbox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Listbox1.MouseDown
'save off the current selection because it doesn't get set in the mousemove or click event
iSelectedIndex = Listbox1.SelectedIndex
End Sub
Private Sub Listview1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragOver
'find the currently hovered item, select it and save it off
Dim cp As Point = Listview1.PointToClient(New Point(e.X, e.Y))
Dim hoverItem As ListViewItem = Listview1.GetItemAt(cp.X, cp.Y)
If Not (hoverItem Is Nothing) Then
e.Effect = DragDropEffects.Copy
iLastItemIndex = hoverItem.Index
Listview1.Items(iLastItemIndex).Selected = True
Else
e.Effect = DragDropEffects.None
End If
End Sub