zigi said:
Hello,
I found the following solution on the web after days of searching:
The way is to put all NOT-selected items in an array, delete the whole
listbox and then fill the listbox aggain with all items from the
array.
Dim ArrSize As Integer
Dim i As Integer
Dim TempFilesArray()
ArrSize = 0
For i = 0 To LstBox.ListCount - 1
If LstBox.Selected(i) = False Then
ArrSize = ArrSize + 1
ReDim Preserve TempFilesArray(ArrSize)
TempFilesArray(ArrSize) = LstBox.ItemData(i)
End If
Next
For i = 0 To LstBox.ListCount - 1
LstBox.RemoveItem (0)
Next i
For i = 1 To ArrSize
LstBox.AddItem TempFilesArray(i)
Next
Here's another approach. I don't know for sure, but I think it might be
marginally more efficient:
Dim strRemoveList As String
Dim astrRemoveItem() As String
Dim varItem As Variant
Dim lngI As Long
With Me.lstSource
For Each varItem In .ItemsSelected
strRemoveList = strRemoveList & Chr(0) & .ItemData(varItem)
Next varItem
If Len(strRemoveList) > 0 Then
astrRemoveItem = Split(Mid$(strRemoveList, 2), Chr(0))
For lngI = LBound(astrRemoveItem) To UBound(astrRemoveItem)
.RemoveItem astrRemoveItem(lngI)
Next lngI
End If
End With
'----- start of code -----
Dim strRemoveList As String
Dim astrRemoveItem() As String
Dim varItem As Variant
Dim lngI As Long
With Me.lstSource
For Each varItem In .ItemsSelected
strRemoveList = strRemoveList & Chr(0) & .ItemData(varItem)
Next varItem
If Len(strRemoveList) > 0 Then
astrRemoveItem = Split(Mid$(strRemoveList, 2), Chr(0))
For lngI = LBound(astrRemoveItem) To UBound(astrRemoveItem)
.RemoveItem astrRemoveItem(lngI)
Next lngI
End If
End With
'----- end of code -----