I am trying to set up a listview where I can reorder the items, but no matter what I do the item i drag and drop goes to the end of the listview. I searched and tried to fix this issue but I just can't seem to find a fix. Any help would be very much appreciated.
Listview Settings:
AllowColumnReorder = True
AllowDrop = True
AutoArrange = True - (Tried with both True and False)
View = LargeIcon
Everything else is pretty much set to defaults
Current Code:
Note: The Listview has no columnsCode:Private Sub Listview1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragDrop 'Return if the items are not selected in the ListView control. If Listview1.SelectedItems.Count = 0 Then Return 'Returns the location of the mouse pointer in the ListView control. Dim p As Point = Listview1.PointToClient(New Point(e.X, e.Y)) 'Obtain the item that is located at the specified location of the mouse pointer. Dim dragToItem As ListViewItem = Listview1.GetItemAt(p.X, p.Y) If dragToItem Is Nothing Then Return 'Obtain the index of the item at the mouse pointer. Dim dragIndex As Integer = dragToItem.Index Dim i As Integer Dim sel(Listview1.SelectedItems.Count) As ListViewItem For i = 0 To Listview1.SelectedItems.Count - 1 sel(i) = Listview1.SelectedItems.Item(i) Next For i = 0 To Listview1.SelectedItems.Count - 1 'Obtain the ListViewItem to be dragged to the target location. Dim dragItem As ListViewItem = sel(i) Dim itemIndex As Integer = dragIndex If itemIndex = dragItem.Index Then Return If dragItem.Index < itemIndex Then itemIndex = itemIndex + 1 Else itemIndex = dragIndex + i End If 'Insert the item in the specified location. Dim insertitem As ListViewItem = dragItem.Clone Listview1.Items.Insert(itemIndex, insertitem) 'Removes the item from the initial location while 'the item is moved to the new location. Listview1.Items.Remove(dragItem) Next End Sub Private Sub Listview1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Listview1.DragEnter Dim i As Integer For i = 0 To e.Data.GetFormats().Length - 1 If e.Data.GetFormats()(i).Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection") Then 'The data from the drag source is moved to the target. e.Effect = DragDropEffects.Move End If Next End Sub Private Sub Listview1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles Listview1.ItemDrag Listview1.DoDragDrop(Listview1.SelectedItems, DragDropEffects.Move) End Sub




Reply With Quote
