delete multiple selected items from listbox

Z

zigi

hello,

how can I delete multiple selected items out of a list box.

thanks, for any help in advance
Regards,
Zigi
 
Z

zigi

Hello Rainbow,

thanks for your answer.
May be I was a littlebit unprecise.
I don't want to delete the selection.

I want to delete each item (list entry) in the listbox, which is selected.
the listbox is an unbound control, which I fill with the 'Listbox.AddItem'
method.

I can remove one Item from the Listbox with the 'Listbox.RemoveItem'.

the following code doesn't work, when I want to remove multiple selected
items:

For Each IdxSource In LstSource.ItemsSelected
LstSource.RemoveItem (IdxSource)
Next IdxSource

Any suggestions how to do this?
 
R

Rainbow01

u try these:

first i think u already set the ListBox.RowSourceType = "Value List"

For Each IdxSource In LstSource.ItemsSelected
LstSource.RemoveItem (IdxSource) <-------- change to:
LstSource.RemoveItem Index:=IdxSource
Next IdxSource



"zigi" 來函:
 
Z

zigi

thank you rainbow again,

but too bat, it didn't work.

If I select two or more items in the listbox, it deletes the first selected
item and then it exits the for-loop.

any other suggestions?
 
Z

zigi

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
 
D

Dirk Goldgar

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 -----
 
Top