Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I delete multiple items in a listbox?

Status
Not open for further replies.

nikischwabe

Programmer
Jun 9, 2000
14
0
0
AU
I've got a feeling this one should be simple but it is causing me a hedache at the moment, and I can't find a relevant FAQ.

I have listbox (multi select) with 'Value List' as a the RowSourceType.

I thought something simple like this might work:

For Each i in listbox.ItemsSelected
listbox.RemoveItem (i)
Next

Of course it isn't that simple because the index changes as soon as the first row is deleted.

Can anyone tell me if there is an easier way to delete multiple selected items in the list?

Or am I having one of those 'thicker than air' days?

Nice one :)
 
What about something like:

ItemsRemoved = 0
For Each i in listbox.ItemsSelected
listbox.RemoveItem (i - ItemsRemoved)
ItemsRemoved = ItemsRemoved + 1
Next

This approach should adjust the index to accomodate the index changes. Hav'nt tried it myself, so keep us posted.

Remember ItemsRemoved is a local variable, so declare it as appropriate.


Cheers,

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
 
Thanks for the response.

Problem not solved though. I think when RemoveItem is first called it deselects the rest of the items in the list, so only one item gets deleted.

I suppose I could load the selected values into an array then loop through the list again turfing out the items that match the array values but this seems awfully clumsy.

Any more Ideas? Nice one :)
 
No, afraid not. Might have to do it the way you propose.
Good luck Cheers,

Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top