Option Explicit
Private Sub Form_Load()
'set the multiselect property of the listbox control (LIST1) to FALSE!
'set the following properties
cmdAdd.Caption = "add"
cmdRemove.Caption = "remove"
cmdUp.Caption = "up"
cmdDown.Caption = "down"
End Sub
Private Sub cmdAdd_Click()
'add an item
List1.AddItem "Item " & CStr(List1.ListCount + 1)
End Sub
Private Sub cmdRemove_Click()
'remove an item
If List1.ListIndex > -1 Then
List1.RemoveItem List1.ListIndex
End If
End Sub
Private Sub cmdUp_Click()
Dim sText As String
Dim iIndex As Integer
'check: only proceed if there is a selected item
If List1.SelCount = 1 Then
'index 0 is top item which can't be moved up!
If List1.ListIndex = 0 Then Exit Sub
'save items text and items indexvalue
sText = List1.List(List1.ListIndex)
iIndex = List1.ListIndex
'remove item
List1.RemoveItem List1.ListIndex
'place item back on new position
List1.AddItem sText, iIndex - 1
'if you keep that item selected
'you can keep moving it by pressing cmdUp
List1.Selected(iIndex - 1) = True
End If
End Sub
Private Sub cmdDown_Click()
Dim sText As String
Dim iIndex As Integer
'check: only proceed if there is a selected item
If List1.SelCount = 1 Then
'check: last item can't be moved down
If List1.ListCount - 1 = List1.ListIndex Then Exit Sub
'save items text and items indexvalue
sText = List1.List(List1.ListIndex)
iIndex = List1.ListIndex
'remove item
List1.RemoveItem List1.ListIndex
'place item back on new position
List1.AddItem sText, iIndex + 1
'if you keep that item selected
'you can keep moving it by pressing cmdDown
List1.Selected(iIndex + 1) = True
End If
End Sub