How can i loop thru a Listbox and remove one item at a time..

Posted on 2005-04-25
Medium Priority
Last Modified: 2011-09-20
I am using a code like below to retrive one item at a time but it causes me problems

For i = 0 To Me.lstListBox2.Items.Count - 1
Next i

i want the code to diplay a msgbox for the each item in the list and then remove it. Ultimatley
i will not display a message box but rather stick some dtabase code ther but for debug it will do.
Question by:AlexPonnath
1 Comment
LVL 43

Accepted Solution

TimCottee earned 2000 total points
ID: 13857326
Hi AlexPonnath,

Do it backwards:

For i = Me.lstListBox2.Items.Count - 1 To 0 Step -1

As you remove the first item, the item index gets reduced, so item 1 becomes item 0 and item 2 becomes item1 and so on. Therefore when you have remove half of your items you have reached the end of the list.

Doing it backwards ensures that your items do not get re-indexed and should allow it to work as you want.

Tim Cottee

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question