Remove Multiple items from ListBox (1 Viewer)

brharrii

Registered User.
Local time
Yesterday, 17:09
Joined
May 15, 2012
Messages
272
I'm trying to setup a listbox so that multiple items may be selected and removed at once. The Listbox Multi Select property is currently set to "Extended" and I have the following code on the onclick event of a command button:


Code:
Private Sub Command12_Click()
Dim i As Integer
 For i = List10.ListCount - 1 To 0 Step -1
    If List10.Selected(i) Then
        List10.RemoveItem i
    End If
 Next i

End Sub

I have tested selecting 4 or so records on the list box and then pushing the remove button but it seems that the only record that is removed is the selected record that is furthest down on the listbox. The other selected records become unselected and must be reselected in order to continue removing records. I'm not sure what I'm doing wrong.

Any ideas?

Thanks!
 

TJPoorman

Registered User.
Local time
Yesterday, 18:09
Joined
Jul 23, 2013
Messages
402
The problem is that once you remove an item, the list refreshes.
You can accomplish what you want by using an array to hold the values of the items selected then remove the items using the array:

Code:
Private Sub Command0_Click()
Dim i As Integer
Dim intCount As Integer
Dim aryValues() As Variant
For i = 0 To List1.ListCount - 1
    If List1.Selected(i) Then
        ReDim Preserve aryValues(intCount)
        aryValues(intCount) = i
        intCount = intCount + 1
    End If
Next i
For i = UBound(aryValues) To 0 Step -1
    List1.RemoveItem aryValues(i)
Next i
End Sub
 

Users who are viewing this thread

Top Bottom