-
I am trying to figure out how to move an item in my list box up and down, say I have 10 items in a list box and I want to move the 5th item to the 4th space and the 4th item to the 5th space. So basically I have 2 command buttons, Up and Down, when the user clicks one or the other it moves the highlighted, or selected item up or down in the listbox. I have had people make suggestions but I can never figure out how to really do it. I appreciate everyones help. Thanks for your time!
AOL Instant Messanger: Korn Child LAPD
Email: [email protected]
-
Here is my help!
It hasn't beg control so you 'll have to do it yourself! But it works i've just tested it
Code:
Private Sub Up_Click()
Dim tempval
With List1
tempval = .List(.ListIndex - 1)
.List(.ListIndex - 1) = .List(.ListIndex)
.List(.ListIndex) = tempval
.Refresh
End With
End Sub
Private Sub Form_Load()
With List1
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
.AddItem "5"
.AddItem "6"
End With
End Sub
Cheers
-
Here is complete, working, code
Code:
Private Sub cmdDown_Click()
Dim intIndex As Integer
Dim strValue As String
For intIndex = 0 To List1.ListCount - 2
If List1.SelCount > 1 Then
MsgBox "Please choose only one entry at a time to move", vbExclamation, "Too many items selected"
Exit Sub
End If
If List1.Selected(intIndex) = True Then
strValue = List1.List(intIndex)
List1.RemoveItem intIndex
List1.AddItem strValue, intIndex + 1
List1.ListIndex = intIndex + 1
Exit For
End If
Next
End Sub
Private Sub cmdUp_Click()
Dim intIndex As Integer
Dim strValue As String
For intIndex = 1 To List1.ListCount - 1
If List1.SelCount > 1 Then
MsgBox "Please choose only one entry at a time to move", vbExclamation, "Too many items selected"
Exit Sub
End If
If List1.Selected(intIndex) = True Then
strValue = List1.List(intIndex)
List1.RemoveItem intIndex
List1.AddItem strValue, intIndex - 1
List1.ListIndex = intIndex - 1
Exit For
End If
Next
End Sub