Results 1 to 8 of 8

Thread: [RESOLVED] Listview Drag & Drop - Items go to end instead of where dragged

  1. #1

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Resolved [RESOLVED] Listview Drag & Drop - Items go to end instead of where dragged

    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:
    Code:
         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
    Note: The Listview has no columns

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    See here: ListBox/ListView Move Item with Mouse
    You use the mouse down and the mouse move events to move items up or down.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    Quote Originally Posted by JuggaloBrotha View Post
    See here: ListBox/ListView Move Item with Mouse
    You use the mouse down and the mouse move events to move items up or down.
    Thank you for the tip, however, when i set that control's view to LargeIcon and run it, the control's view reverts back to Details. So, it's not working for my purpose, any other suggestions?

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    I sent you that as an example of how you'd move items around with the mouse, that control I made doesn't support views other than Detail. Eventually I might get around to supporting those other views.

    But specifically, check out the MouseDown & MouseMove events in that control to see how it actually moves items.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    Quote Originally Posted by JuggaloBrotha View Post
    I sent you that as an example of how you'd move items around with the mouse, that control I made doesn't support views other than Detail. Eventually I might get around to supporting those other views.

    But specifically, check out the MouseDown & MouseMove events in that control to see how it actually moves items.
    The code I posted with the original post already supported moving items around with the mouse in views like details, etc. So, I'm already familiar with how it's done. I'd appreciate it if someone can offer some help with this.

  6. #6

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    After much researching and tweaking, I was able to resolve this issue as follows:

    Put in Form Load
    Code:
    ListView1.ListViewItemSorter = New ListViewIndexComparer()
    Add the following class
    Code:
        ' Sorts ListView1 objects by index.    
        Private Class ListViewIndexComparer
            Implements System.Collections.IComparer
    
            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
                Implements System.Collections.IComparer.Compare
                Return CType(x, ListView1).Index - CType(y, ListView1).Index
            End Function 'Compare
        End Class
    Last edited by SavedByGrace; Jul 2nd, 2010 at 08:17 PM.

  7. #7
    New Member
    Join Date
    May 2016
    Posts
    1

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    Quote Originally Posted by JuggaloBrotha View Post
    See here: ListBox/ListView Move Item with Mouse
    You use the mouse down and the mouse move events to move items up or down.
    Hello, this is an old post. However, I'm trying to get this to work with items inside groups. When items inside groups are moved they
    are moved to the Default group.
    Below is my attempt to fix where I think the problem lays.

    Code:
                            'Dim lastItemBottom As Integer = Math.Min(e.Y, (Me.Groups(_grpIdx).Items(Me.Groups(_grpIdx).Items.Count - 1I).GetBounds(ItemBoundsPortion.Entire).Bottom - 1I))
    Here is your original solution
    Code:
                            Dim lastItemBottom As Integer = Math.Min(e.Y, Me.Items(Me.Items.Count - 1I).GetBounds(ItemBoundsPortion.Entire).Bottom - 1I)
    The overall purpose of this in my application is to allow a table of contents "feel" to the listview.

    Any suggestions?

    Thank you in advance

  8. #8

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Listview Drag & Drop - Items go to end instead of where dragged

    Here is your original solution
    Code:
                            Dim lastItemBottom As Integer = Math.Min(e.Y, Me.Items(Me.Items.Count - 1I).GetBounds(ItemBoundsPortion.Entire).Bottom - 1I)
    I'm not sure if you are posting to the right thread. The code that you posted as my "original solution" is not a part of the thread at all. Also, I had posted the solution to the problem I was having, if that solution doesn't work for you or is different than what you need, you may want to open a new thread as this one is 6 years old and no longer active. Good luck.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width