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 from one position in a list to another
Keywordslist, drag, drag and drop
CategoriesControls
 
Use drag and drop. When the user clicks on the list, the list's MouseDown event handler starts a drag.

If the user drops an item on the form, the Form_DragDrop event handler removes the item from the list.

If the user drops an item on the list, the List1_DragDrop event handler moves the item to its new position.

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

Private Sub Form_DragDrop(Source As Control, X As Single, Y _
    As Single)
    If Not (Source Is List1) Then Exit Sub
    If List1.ListIndex >= 0 Then List1.RemoveItem _
        List1.ListIndex
End Sub

Private Sub List1_DragDrop(Source As Control, X As Single, _
    Y As Single)
Dim new_pos As Integer

    If Not (Source Is List1) Then Exit Sub

    ' Add the item in its new position.
    new_pos = Y / item_height
    If new_pos > List1.ListCount Then
        List1.AddItem List1.List(List1.ListIndex)
    Else
        List1.AddItem List1.List(List1.ListIndex), new_pos
    End If

    ' Remove the item from its old position.
    If List1.ListIndex >= 0 Then List1.RemoveItem _
        List1.ListIndex
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated