[Resolved] Move item inside listbox (up and down)
Hey, anyone with a simple code for moving items up or down in a lsitbox?
I mean if you have the items 1 - 5 in a listbox,and you would like
the item5 to become item2, then you simply click item5, hold button and drop where item2 is and then we get a list that looks like this:
NEW: ----- ORGINAL:
item1 ------ item1
item5 ------ item2
item2 ------ item3
item3 ------ item4
item4 ------ item5
Do you understand what i want? lets say its equal to winamp's behavior, for example...
Please help! PS: keep the code as simple as possible!
Here's one way to do it...
Could use a little polishing, but this should get you started.- First, set the OLEDragMode property of the ListBox to Automatic
- Second, set the OLEDopMode of the ListBox to Manual
This will allow you to capture drag and drop events in the list box.
Then, you should create some global variables to capture what the user is doing and what data they are playing with. I created the following:- Variable to track the text of the last item selected in the list box
- Variable to track the index of the last item selected in the list box
- Variable to track if the user is dragging from our list box (as opposed to some other source)
Then, I added code into the OLEDragDrop event to determine where in the list box the user was dropping, then I removed the index they originally selected and added the text at the index where they were dropping.
Here's the code, hope it helps:
VB Code:
Option Explicit
' Module level variables hold information about dragging stuff in the list box
Private DragData As String
Private DragFromLocation As Integer
Private IsDragging As Boolean
Private Sub Form_Load()
' Load some fake data for testing
List1.Clear
List1.AddItem "one"
List1.AddItem "two"
List1.AddItem "three"
List1.AddItem "four"
List1.AddItem "five"
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
' Always save the text and index of the last item clicked on because it may be dragged
DragData = List1.List(List1.ListIndex)
DragFromLocation = List1.ListIndex
End If
End Sub
Private Sub List1_OLECompleteDrag(Effect As Long)
' User is done dragging an item from the list box, so set our flag
IsDragging = False
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 DropLocation As Integer
' Make sure we are the source for the drag
If IsDragging Then
' Figure out how many items down in the list (the index) where we need to add this item
DropLocation = Int(Y / Me.TextHeight("Text"))
If DropLocation < 0 Then DropLocation = 0
If DropLocation >= List1.ListCount Then DropLocation = List1.ListCount - 1
' Remove the dragged item
List1.RemoveItem DragFromLocation
' And insert it in the new position
List1.AddItem DragData, DropLocation
' And we are no longer dragging...
IsDragging = False
End If
End Sub
Private Sub List1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
' User is dragging an item from the list box, so set our flag
IsDragging = True
End Sub