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 items between two ListBoxes
Keywordsdrag and drop, ListBox
CategoriesControls
 
The code is the same for both ListBoxes so only one set of event handlers is described here.

In a ListBox's MouseDown event handler, call the control's Drag method with the parameter vbBeginDrag. In the control's DrawOver event handler, display an appropriate drop icon. In the DragDrop event handler, move the selected item from one list to the other if the drop is coming from the right control.

Finally, in the Form's DragOver event handler, display the "no drop" icon.

 
' Start dragging.
Private Sub List1_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    List1.Drag vbBeginDrag
End Sub

' Display an appropriate drop picture.
Private Sub List1_DragOver(Source As Control, X As Single, _
    Y As Single, State As Integer)
    If Source Is List2 Then
        ' Allow drop from List2.
        Source.DragIcon = picDocument.Picture
    Else
        ' Do not allow drop from anything else.
        Source.DragIcon = Me.picNoDrop.Picture
    End If
End Sub

' Remove the item dragged from List2 and
' add it to List1.
Private Sub List1_DragDrop(Source As Control, X As Single, _
    Y As Single)
    If Source Is List2 Then
        List1.AddItem List2.List(List2.ListIndex)
        List2.RemoveItem List2.ListIndex
    End If
End Sub

' Display the "no drop" icon.
Private Sub Form_DragOver(Source As Control, X As Single, Y _
    As Single, State As Integer)
    Source.DragIcon = picNoDrop.Picture
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated