article

A very useful method for removing items from listboxes in for loops

Email
Submitted on: 2/12/2015 5:39:00 PM
By: Sean Patrick Kane (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 676
 
     Many people, at some point in their VB programming careers, will use a for-loop to remove items from a listbox. I would say that easily 90% of the people that code for a situation like this are coding it INCORRECTLY and are using extremely poor programming style. I would encourage beginners and experts alike to examine this article to learn the best way to use a for loop to remove listbox items.

 
				Before We Begin

This is something that every programmer should learn! The situation I'm going to present assumes that we have one listbox on our form, named List1, that has any number of items within it. For whatever reason, we want to loop through the listbox, and remove certain items -- say, any items that have the letter "B" in it; however, please realize, a much more common use of this code would be to search for duplicate items, or items that match a specific need or requirement. I'm going to show you the common way of doing things, which is extremely ugly and incorrect, and then I'm going to demonstrate the proper way to go about the situation I have presented.

The Wrong (but most common) Method
Before any programmer attempts to debug their application, they may write something like this:

Dim i as integer 'Declare our variables
For i = 0 to List1.ListCount - 1 'Loop through the listbox

    If InStr(List1.List(i), "B") <> 0 Then'B exists in this item...we need to remove it
    List1.RemoveItem i 'Remove the offending item
    End If
Next i

Unfortunately, once the program ran, the coder would be presented with an error message saying "Run-time error '13': Type mismatch". What does this programmer do? All too often, they just throw in a line similar to "On Error Resume Next", and magically the error message goes away, and usually it doesn't affect the actual function of the problematic function. This is, of course, a really bad way to troubleshoot a problem. Before fixing the problem, we should understand what the problem actually is...

Why The Error Message?
If the coder would use the debug stuff in VB, they would find that their program is trying to address an index (the i variable) that no longer exists -- often times the i variable equals the List1.ListCount. Basically what has happened is that the For loop stores the ListCount when it starts, and never re-initializes it. For instance, if List1 contains 5 items before the for-loop runs, and during the loop we remove 2 items, the for-loop would still be running as "For i = 0 to 5", rather than "For i = 0 to 3" (since we removed two items). This of course will cause an error once we try to access List1.List(4) or List1.List(5) because those items will not exist. Now that we know the problem, we can rid ourselves of that nasty "On Error Resume Next" line and actually code the thing correctly.

The Correct (but unfortunately uncommon) Method
There is a very, very simple way to correct this problem. Instead of having our for-loop start at the beginning of the listbox and move to the end, all we have to do is start at the end and move to the beginning. If we code the for-loop this way, we will never try to access an invalid index. The corrected code is shown below:

Dim i as integer 'Declare our variables
For i = (List1.ListCount - 1) to 0 Step -1 'Loop through the listbox backwards

    If InStr(List1.List(i), "B") <> 0 Then'B exists in this item...we need to remove it
    List1.RemoveItem i 'Remove the offending item
    End If
Next i

Conclusion
Using the new method, any number of listbox items can be removed safely without the problem of addressing invalid index items. I realize this is a fairly basic concept, but I've seen too many PSC submissions that were using the incorrect method. Any comments or votes would be greatly appreciated.


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.