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

Posted on 2005-04-25
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 44

    Accepted Solution

    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

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now