What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDrag and drop objects from one ListBox to another in Visual Basic .NET
DescriptionThis example shows how to drag and drop objects from one ListBox to another in Visual Basic .NET.
Keywordsdrag, drop, drag and drop, objects, ListBox, Visual Basic .NET, VB .NET
In this example, two ListBoxes contain Person objects. The Person class's ToString method is overridden to display the person's first and last names.

When you press the right mouse button over a ListBox, the MouseDown event handler starts the drag by calling the control's DoDragDrop method. It passes in the selected string to drag and allows the copy and move actions.

The call to DoDragDrop returns when the drag is finished. The method returns a value indicating whether the drag resulted in a move, copy, or nothing (in this case). If the drag ended with a move, then the code removes the item from the ListBox that started the drag.

' Start a drag.
Private Sub ListBox_MouseDown(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    lstNaughty.MouseDown, lstNice.MouseDown
    Dim lst As ListBox = DirectCast(sender, ListBox)

    If e.Button = Windows.Forms.MouseButtons.Right Then
        Dim index As Integer = lst.IndexFromPoint(e.X, e.Y)
        If index <> ListBox.NoMatches Then
            Dim per As Person = lst.Items(index)
            Dim drop_effect As DragDropEffects = _
                lst.DoDragDrop( _
                    per, _
                    DragDropEffects.Move Or _

            ' If it was moved, remove the item from this
            ' list.
            If drop_effect = DragDropEffects.Move Then
            End If
        End If
    End If
End Sub
If the drag moves over a ListBox, the DragOver event gives the control a chance to allow or prohibit the drag. The following code checks whether the drag can provide an object of type howto_net_drag_drop_listbox_objects.Person as data. (The name is the program's namespace followed by the class name.) If the drag can provide this data, then the code checks whether the Ctrl key is down and allows a move or copy effect.
' Display the appropriate cursor.
Private Sub ListBox_DragOver(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.DragEventArgs) Handles _
    lstNaughty.DragOver, lstNice.DragOver
    Const KEY_CTRL As Integer = 8

    If Not (e.Data.GetDataPresent("howto_net_drag_drop_listbox_objects.Person")) _
        e.Effect = DragDropEffects.None
    ElseIf (e.KeyState And KEY_CTRL) And _
    (e.AllowedEffect And DragDropEffects.Copy) = _
        DragDropEffects.Copy Then
        ' Copy.
        e.Effect = DragDropEffects.Copy
    ElseIf (e.AllowedEffect And DragDropEffects.Move) = _
        DragDropEffects.Move Then
        ' Move.
        e.Effect = DragDropEffects.Move
    End If
End Sub
Finally, when the user drops, the DragDrop event handler deals with the dropped data. If the drop can provide a Person object and this is a copy or move, the code gets the data, converts it into a Person, and adds it to the receiving ListBox.

The event handler's e parameter gives the location where the drop occurred in screen coordinates. The code uses the control's PointToClient method to convert the coordinates into the control's coordinate system. It then uses the IndexFromPoint method to learn the index of the item where the drop occurred. Finally the code inserts the dropped string before this item.

' Drop the entry in the list.
Private Sub ListBox_DragDrop(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.DragEventArgs) Handles _
    lstNaughty.DragDrop, lstNice.DragDrop
    If e.Data.GetDataPresent("howto_net_drag_drop_listbox_objects.Person") _
        If (e.Effect = DragDropEffects.Copy) Or _
        (e.Effect = DragDropEffects.Move) Then
            Dim lst As ListBox = DirectCast(sender, ListBox)
            Dim item As Person = DirectCast( _
                e.Data.GetData("howto_net_drag_drop_listbox_objects.Person"), _
            item = New Person(item.FirstName, item.LastName)
            Dim pt As Point = lst.PointToClient(New _
                Point(e.X, e.Y))
            Dim index As Integer = lst.IndexFromPoint(pt.X, _
            If index = ListBox.NoMatches Then
                lst.Items.Insert(index, item)
            End If
        End If
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.