Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

VS 2005 [RESOLVED] Problem with drag and drop reordering of listview items

$
0
0
:wave:
I have a listview that is in details view. I want to be able to have drag and drop reordering of items. I got the following code from Microsoft that is supposed to do this very thing:
Code:

    Private Sub ScriptBrowserListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ScriptBrowserListView.DragDrop

        'Return if the items are not selected in the ListView control.
        If ScriptBrowserListView.SelectedItems.Count = 0 Then Return

        'Returns the location of the mouse pointer in the ListView control.
        Dim p As Point = ScriptBrowserListView.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 = ScriptBrowserListView.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(ScriptBrowserListView.SelectedItems.Count) As ListViewItem

        For i = 0 To ScriptBrowserListView.SelectedItems.Count - 1
            sel(i) = ScriptBrowserListView.SelectedItems.Item(i)
        Next

        For i = 0 To ScriptBrowserListView.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
            ScriptBrowserListView.Items.Insert(itemIndex, insertitem)

            'Removes the item from the initial location while
            'the item is moved to the new location.
            ScriptBrowserListView.Items.Remove(dragItem)

        Next

    End Sub

    Private Sub ScriptBrowserListView_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ScriptBrowserListView.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 ScriptBrowserListView_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ScriptBrowserListView.ItemDrag
        ScriptBrowserListView.DoDragDrop(ScriptBrowserListView.SelectedItems, DragDropEffects.Move)
    End Sub

Problem is... it doesn't work - that is to say, it doesn't perform a drag-drop operation. I can tell it's doing something though because when I try to perform a drag-drop, it changes the mouse cursor to a circle with a line through it. It doesn't throw any exceptions, though.

I've never tried to do drag-drop before, so I have no idea where to begin.

Any ideas?

Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>