Results 1 to 7 of 7

Thread: Dragging items within a ListView

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Dragging items within a ListView

    Code:
    Public Class Form1
    
        Dim lw As ListViewItem 'Dragging ListViewItem
        Dim yf, yl, i, ii, iii, indx As Integer 'yf:Yaxis first, yl:Yaxis last, i:new index, ii:half of the row height, iii:itemscount, indx:index of lw
        Dim flag As Boolean 'flag: if mouse button is being pressed
    
        Private Sub ListView1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDown
            iii = ListView1.Items.Count
            If iii > 0 Then 'if list is not empty
                Dim rc As Rectangle = ListView1.Items(0).GetBounds(ItemBoundsPortion.Entire) 'selected row's height
                flag = True 'button pressed
                yf = e.Y 'start point of mousemove to detect if movement is up or down
                ii = Math.Round(rc.Height / 2)  'it is the limit needed to move the item.
                i = 0 'default selected index is 0
            End If
        End Sub
    
        Private Sub ListView1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListView1.MouseMove
            If flag Then 'if button pressed
                yl = e.Y 'get the actual mouse position
                If Math.Abs(yl - yf) > ii Then 'if the movement is more than the half of the row
                    lw = ListView1.SelectedItems(i)
                    indx = lw.Index
                    ListView1.Items.Remove(lw)
                    Select Case yl
                        Case Is > yf 'if the movement is downwards
                            If indx < iii - 1 Then 'if it is not the last item
                                i += 1 'new index will be
                                yf = yl + ii 'some margin that i can't explain with my english :)
                            End If
                        Case Else 'if the movement is upwards
                            If indx > 0 Then 'if it is the not the first item
                                i -= 1
                                yf = yl - ii
                            End If
                    End Select
                    ListView1.Items.Insert(indx + i, lw) 'insert to i value up or down
                    i = 0
                End If 'don't move anything anymore unless the condition "If Math.Abs(yl - yf) > iii" is provided somewhere else above.
            End If
        End Sub
    
        Private Sub ListView1_MouseUp(sender As Object, e As MouseEventArgs) Handles ListView1.MouseUp
            flag = False 'mouse is not being pressed anymore
        End Sub
    
    End Class
    Last edited by Flashbond; Feb 15th, 2014 at 11:02 AM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

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

    Re: Dragging items within a ListView

    This looks like a great start, have you thought of wrapping this into a self contained control? Maybe add some move line indication(s)?
    Check out ListView Mouse Move Item for an example.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  3. #3

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Dragging items within a ListView

    Yeah, actually I have inspired from that A Wonderful work from you and .paul.
    I downloaded and examined it in detail for a night.
    But the thing is I didn't like that red line thing, really, no heartbreaks
    Maybe, it can be a mixture of yours' and mine. Infact the real look is usually (in winamp playlists, etc...), it highligths the row string next to cursor like yours, opens an empty row into the new position during dragging like mine. Mine is still insufficent to open a new empty row for new replacement. All it does is only swaping indexes.
    In your code the replacement happening via mouseup which was the second thing that I wanted to change actually. I tried to catch the realtime-dragging feeling.

    Secondly, I am not that good programmer. Infact I am in travel business. I make programs only for my office needs. So I don't know how to make it a self contained control like you did If you advance my code, I would appreciate

    If you really found my effort worthy then it may be very nice to give me rep It really encourages me for the future works

    PS: I wrote it before in another post, my code needs a fundemantal improvement. There occures some lacks in fast moves and congested list. This is my main problem for my daily usage for now.
    Last edited by Flashbond; Feb 12th, 2014 at 12:56 PM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

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

    Re: Dragging items within a ListView

    Perhaps that winamp idea is something you could take on (I actually use winamp, have for years, honestly I never even noticed how it swaps items in realtime like that till you mentioned it here).
    I posted the link to mine simply as an example for ya, since they both do similar things, hopefully it'll be something you can be inspired from, use it to help you make something even better.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  5. #5

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Dragging items within a ListView

    Thanks again
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Dragging items within a ListView

    @Flashbond,

    Nice code however, maybe you could edit you first post to include a description of what the code does and the exact requirements need in order to create a working demo using the code.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Dragging items within a ListView

    @Nightwalker83

    In fact, it does nothing more than what does it promise in the title. Actually it requires only a ListView control to able to drag items within it...
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

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