Drag and Drop ListBox items

This example shows you how to enable the user to drop and drag items between two list boxes. Add two listboxes to your form, and insert the following code.

Private Sub Form_Load()
    ' Populate the list
    List1.AddItem "James"
    List1.AddItem "Frederick"
    List1.AddItem "Ann"
    List1.AddItem "Paul"
    List1.AddItem "Sarah"
    List1.OLEDropMode = 1
    List2.OLEDropMode = 1
End Sub

' Code managing dropping from list one
' to list two
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    List1.OLEDrag    ' Begin dragging
End Sub
Private Sub List1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    ' Only allow moves
    AllowedEffects = vbDropEffectMove
    ' Assign the ListBox selection to the DataObject
    Data.SetData List1
End Sub
Private Sub List2_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim strList As String
    ' Check the format of the DataObject
    If Not Data.GetFormat(vbCFText) Then Exit Sub
    ' Retrieve the text from the DataObject
    strList = Data.GetData(vbCFText)
    ' If the item was not dropped on itself
    If Not strList = List2.Text Then
        List2.AddItem strList
        'Remove the item from the ListBox
        List1.RemoveItem List1.ListIndex
    End If
End Sub
''
''
'' Code managing dropping from list one
'' to list two
''
Private Sub List2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    List2.OLEDrag    ' Begin dragging
End Sub
Private Sub List2_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    ' Only allow moves
    AllowedEffects = vbDropEffectMove
    ' Assign the ListBox selection to the DataObject
    Data.SetData List2
End Sub
Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim strList As String
    ' Check the format of the DataObject
    If Not Data.GetFormat(vbCFText) Then Exit Sub
    ' Retrieve the text from the DataObject
    strList = Data.GetData(vbCFText)
    ' If the item was not dropped on itself
    If Not strList = List1.Text Then
        List1.AddItem strList
        'Remove the item from the ListBox
        List2.RemoveItem List2.ListIndex
    End If
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson