Results 1 to 8 of 8

Thread: List box drag and drop

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    35

    List box drag and drop

    Hi, the following code allows me to drag and drop the data, so i can reorder the list box. However, is there are way of seeing the line across the list box where it will be moved to? Thanks

    Code:
    Private Sub lstteam1players_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstteam1players.DragDrop
        lstteam1players.Items.Insert(lstteam1players.IndexFromPoint(lstteam1players.PointToClient(New Point(e.X, e.Y))), e.Data.GetData(DataFormats.Text))
        lstteam1players.Items.RemoveAt(lstteam1players.SelectedIndex)
    End Sub
    
    Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstteam1players.DragOver
        e.Effect = DragDropEffects.Move
    End Sub
    
    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstteam1players.MouseDown
        lstteam1players.DoDragDrop(lstteam1players.Text, DragDropEffects.All)
    End Sub

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

    Re: List box drag and drop

    I've got a ListView that does this (draws an insertion line and moves it as the mouse moves) I'll see about applying the concept to a ListBox.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    35

    Re: List box drag and drop

    Thanks that would be great! It just looks a bit messy without seeing where the data will be moved to

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: List box drag and drop

    Try this extended listbox control:

    vb Code:
    1. Public Class listboxEx
    2.     Inherits ListBox
    3.  
    4.     Private Const WM_PAINT As Integer = &HF
    5.     Private Const WM_MOUSEMOVE = &H200
    6.  
    7.     ' The Item being dragged
    8.     Private _itemDnD As Object = Nothing
    9.  
    10.     Private itemOver As Integer = -1
    11.  
    12.     Public Sub New()
    13.         ' Reduce flicker
    14.         MyBase.DoubleBuffered = True
    15.     End Sub
    16.  
    17.     Protected Overloads Overrides Sub WndProc(ByRef m As Message)
    18.         MyBase.WndProc(m)
    19.  
    20.         If m.Msg = WM_MOUSEMOVE And _itemDnD IsNot Nothing OrElse m.Msg = WM_PAINT Then
    21.             If itemOver >= 0 AndAlso itemOver < Items.Count Then
    22.                 Dim rc As Rectangle = New Rectangle(0, MyBase.GetItemRectangle(itemOver).Top, MyBase.GetItemRectangle(itemOver).Width, MyBase.GetItemHeight(0))
    23.                 DrawInsertionLine(rc.Left, rc.Right, rc.Top)
    24.             End If
    25.         End If
    26.  
    27.     End Sub
    28.  
    29.     ''' <summary>
    30.     ''' Draw a line with insertion marks at each end
    31.     ''' </summary>
    32.     ''' <param name="X1">Starting position (X) of the line</param>
    33.     ''' <param name="X2">Ending position (X) of the line</param>
    34.     ''' <param name="Y">Position (Y) of the line</param>
    35.     Private Sub DrawInsertionLine(ByVal X1 As Integer, ByVal X2 As Integer, ByVal Y As Integer)
    36.         Using g As Graphics = MyBase.CreateGraphics()
    37.             g.DrawLine(Pens.Red, X1, Y, X2 - 1, Y)
    38.             Dim leftTriangle As Point() = New Point(2) {New Point(X1, Y - 4), New Point(X1 + 7, Y), New Point(X1, Y + 4)}
    39.             Dim rightTriangle As Point() = New Point(2) {New Point(X2, Y - 4), New Point(X2 - 8, Y), New Point(X2, Y + 4)}
    40.             g.FillPolygon(Brushes.Red, leftTriangle)
    41.             g.FillPolygon(Brushes.Red, rightTriangle)
    42.         End Using
    43.     End Sub
    44.  
    45.     Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
    46.         MyBase.OnMouseDown(e)
    47.         _itemDnD = MyBase.Items(MyBase.IndexFromPoint(New Point(e.X, e.Y)))
    48.     End Sub
    49.  
    50.     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    51.         MyBase.OnMouseMove(e)
    52.  
    53.         If _itemDnD Is Nothing Then
    54.             Return
    55.         End If
    56.  
    57.         ' Show the user that a drag operation is happening
    58.         Cursor = Cursors.Hand
    59.  
    60.         ' use 0 instead of e.X so that you don't have to keep inside the columns while dragging
    61.         itemOver = MyBase.IndexFromPoint(New Point(0, e.Y))
    62.  
    63.         ' invalidate the listbox
    64.         MyBase.Invalidate()
    65.     End Sub
    66.  
    67.     Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
    68.         MyBase.OnMouseUp(e)
    69.         If _itemDnD Is Nothing Then
    70.             Return
    71.         End If
    72.  
    73.         Try
    74.  
    75.             ' use 0 instead of e.X so that you don't have to keep inside the columns while dragging
    76.             itemOver = MyBase.IndexFromPoint(New Point(0, e.Y))
    77.  
    78.             If itemOver = -1 Then
    79.                 Return
    80.             End If
    81.  
    82.             If _itemDnD IsNot MyBase.Items(itemOver) Then
    83.                 MyBase.Items.Remove(_itemDnD)
    84.                 MyBase.Items.Insert(itemOver, _itemDnD)
    85.             End If
    86.  
    87.             MyBase.SelectedIndex = itemOver
    88.  
    89.             MyBase.Invalidate()
    90.         Finally
    91.             ' finish drag&drop operation
    92.             _itemDnD = Nothing
    93.             Cursor = Cursors.Default
    94.         End Try
    95.     End Sub
    96.  
    97. End Class


    edit: there's a link to an improved version in my signature
    Last edited by .paul.; Feb 10th, 2010 at 12:55 AM. Reason: improvement

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

    Re: List box drag and drop

    Quote Originally Posted by 04petersric View Post
    Thanks that would be great! It just looks a bit messy without seeing where the data will be moved to
    Sorry it took so long, but I did have time earlier this week to make a ListBox that does this.

    While paul's is a great start, there are a few cases where the insertion line wont be removed from the control when the mouse button is released and I his doesn't let go of the item (so to speak) if the mouse leaves the control altogether which I was able to produce a crash.

    I also added an ItemMoved event which provides info about the item that moved, what it's old index was and the index of where it got moved to. Plus the ability to turn the feature on/off by changing a property and the ability to change the Insertion line color via a property.

    More info and the code classes here: ListBox/ListView Move Item with Mouse
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: List box drag and drop

    Try my latest version, I added cursor effects.

    @JB: If you want to improve your code, add multiple items support

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

    Re: List box drag and drop

    Quote Originally Posted by .paul. View Post
    Try my latest version, I added cursor effects.
    One of the reasons I still made this control was for the adding of properties and the event to know when an item was moved, yours still doesn't have that, plus the two issues I'd noticed yours was doing that it shouldn't have been so I sat down and started from scratch, using the code from my LV control for consistency.
    Quote Originally Posted by .paul. View Post
    @JB: If you want to improve your code, add multiple items support
    That's next on my list, getting the first rendition is all I was aiming for here. IE no crashes, handling all the internal events needed for it to perform correctly, etc.

    Next I'm going to try and get both the LB and LV to do this with multiple items selected, I'd like to try and have it do this with multiple random items selected as in not all of the selected items are right next to each other.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

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

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: List box drag and drop

    You could have a property - PreserveSpacing - or something, so you'd give your user a choice.

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