VB6 Snippet - Move Items In A Listbox
I've seen lots of people asking for a way to do this so here is an extremely simple function to rearrange the items in a listbox:
VB Code:
Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, NewIndex As Integer)
Dim strItem As String
strItem = List1.List(CurrentIndex)
List1.RemoveItem CurrentIndex
List1.AddItem strItem, NewIndex
End Function
Re: VB6 Snippet - Move Items In A Listbox
Here's the code so you wont get an error if you have it set to be using a command button to move an item in the listbox and someone tries to move it higher or lower then it's supposed to go.
VB Code:
Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, NewIndex As Integer)
Dim strItem As String
If List1.ListCount = NewIndex Or NewIndex = -1 Then Exit Function
strItem = List1.List(CurrentIndex)
List1.RemoveItem CurrentIndex
List1.AddItem strItem, NewIndex
List1.ListIndex = NewIndex
End Function
This one here i changed so basically you just set it as True if you want to move the listitem up, if you want it to go down then set it to false
VB Code:
Private Function MoveListboxItem(List1 As ListBox, CurrentIndex As Integer, Up As Boolean)
Dim strItem As String
Dim NewIndex As Integer
If Up = True Then
NewIndex = CurrentIndex - 1
Else
NewIndex = CurrentIndex + 1
End If
If List1.ListCount = NewIndex Or NewIndex < 0 Then Exit Function
strItem = List1.List(CurrentIndex)
List1.RemoveItem CurrentIndex
List1.AddItem strItem, NewIndex
List1.ListIndex = NewIndex
End Function
Re: VB6 Snippet - Move Items In A Listbox
More tricks for moving items around in a Listbox.
VB Code:
Private Sub cmdUp_Click()
' only if the first item isn't the current one
If List1.ListIndex >= 0 Then
' add a duplicate item up in the listbox
List1.AddItem List1.Text, List1.ListIndex - 1
' make it the current item
List1.ListIndex = List1.ListIndex - 2
' delete the old occurrence of this item
List1.RemoveItem List1.ListIndex + 2
End If
End Sub
Private Sub cmdDown_Click()
' only if the last item isn't the current one
If List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1 Then
' add a duplicate item down in the listbox
List1.AddItem List1.Text, List1.ListIndex + 2
' make it the current item
List1.ListIndex = List1.ListIndex + 2
' delete the old occurrence of this item
List1.RemoveItem List1.ListIndex - 2
End If
End Sub
Private Sub List1_Click()
' enable/disable buttons when the current item changes
cmdUp.Enabled = (List1.ListIndex > 0)
cmdDown.Enabled = (List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1)
End Sub
'If you want to enable and disable the two buttons as soon as you show the form,
'remember to set the ListBox's ListIndex property explicitly in the Form_Load event
'to a value other than -1, or manually call the List1_Click procedure:
Private Sub Form_Load()
' because there is no current item (ListIndex = -1) you
' must call the Click procedure manually
Call List1_Click
End Sub
Re: VB6 Snippet - Move Items In A Listbox
Thanks! Saved me from having to ask my Q!
Re: VB6 Snippet - Move Items In A Listbox
There you have it! Solutions ranging from v. simple to v. useful! :D
Re: VB6 Snippet - Move Items In A Listbox
Hack, your code seems to have bug, so here is my modification
vb Code:
Private Sub cmdUp_Click()
Dim currIndex As Integer
Dim currItem As String
Dim bSel As Boolean
If lbItems.ListIndex >= 1 Then
currIndex = lbItems.ListIndex
currItem = lbItems.List(currIndex)
bSel = lbItems.Selected(currIndex)
lbItems.RemoveItem currIndex
lbItems.AddItem currItem, currIndex - 1
lbItems.ListIndex = currIndex - 1
lbItems.Selected(currIndex - 1) = bSel
End If
End Sub
Private Sub cmdDown_Click()
Dim currIndex As Integer
Dim currItem As String
Dim bSel As Boolean
If lbItems.ListIndex <> -1 And lbItems.ListIndex < lbItems.ListCount - 1 Then
currIndex = lbItems.ListIndex
currItem = lbItems.List(currIndex)
bSel = lbItems.Selected(currIndex)
lbItems.RemoveItem currIndex
lbItems.AddItem currItem, currIndex + 1
lbItems.ListIndex = currIndex + 1
lbItems.Selected(currIndex + 1) = bSel
End If
End Sub