Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDrag and drop strings from one ListBox to another in Visual Basic .NET
DescriptionThis example shows how to drag and drop strings from one ListBox to another in Visual Basic .NET
Keywordsdrag, drop, drag and drop, ListBox, Visual Basic .NET, VB .NET
CategoriesControls
 
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 lst_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    lstFruits.MouseDown, lstAnimals.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 item As String = lst.Items(index)
            Dim drop_effect As DragDropEffects = _
                lst.DoDragDrop( _
                    lst.Items(index), _
                    DragDropEffects.Move Or _
                        DragDropEffects.Copy)

            ' If it was moved, remove the item from this
            ' list.
            If drop_effect = DragDropEffects.Move Then
                ' See if the user dropped the item in this
                ' ListBox
                ' at a higher position.
                If lst.Items(index) = item Then
                    ' The item has not moved.
                    lst.Items.RemoveAt(index)
                Else
                    ' The item has moved.
                    lst.Items.RemoveAt(index + 1)
                End If
            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 a string as data. If so, then it checks whether the Ctrl key is down and allows a move or copy effect.
 
' Display the appropriate cursor.
Private Sub lstFruits_DragOver(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles _
    lstFruits.DragOver, lstAnimals.DragOver
    Const KEY_CTRL As Integer = 8

    If Not (e.Data.GetDataPresent(GetType(System.String))) _
        Then
        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 string and this is a copy or move, the code gets the string 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 lstFruits_DragDrop(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles _
    lstFruits.DragDrop, lstAnimals.DragDrop
    If e.Data.GetDataPresent(GetType(System.String)) Then
        If (e.Effect = DragDropEffects.Copy) Or _
        (e.Effect = DragDropEffects.Move) Then
            Dim lst As ListBox = DirectCast(sender, ListBox)
            Dim item As Object = _
                CType(e.Data.GetData(GetType(System.String)), _
                System.Object)
            Dim pt As Point = lst.PointToClient(New _
                Point(e.X, e.Y))
            Dim index As Integer = lst.IndexFromPoint(pt.X, _
                pt.Y)
            If index = ListBox.NoMatches Then
                lst.Items.Add(item)
            Else
                lst.Items.Insert(index, item)
            End If
        End If
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated