Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 291311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:04:39+00:00 2026-05-12T06:04:39+00:00

I am trying to filter a ListBox based on the presence of a string.

  • 0

I am trying to filter a ListBox based on the presence of a string. Basically, if there is a ListItem that doesn’t contain the string then I want to remove all ListItems that do contain the string. Here is what I have tried:

Dim Item As ListItem
For Each Item In CtheList.Items
    If Item.Text.IndexOf("W:") = -1 Then
        CtheList.Items.Remove(Item)
     End If
 Next

Which is apparently a no-no as it generates the error: Collection was modified; enumeration operation may not execute.

I have also tried:

Dim Item As ListItem
For Each Item In CtheList.Items
    If Item.Text.IndexOf("W:") = -1 Then
        Dim i As Integer
        For i = 0 To CtheList.Items.Count - 1
            If CtheList.Items.Item(i).Text.IndexOf("W:") > -1 Then
                CtheList.Items.RemoveAt(i)
            End If
        Next i
    End If
Next

Which generates an index out of range exception.

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:04:39+00:00Added an answer on May 12, 2026 at 6:04 am

    Try reversing your loop, i.e. start from the end of the list. That way, deleting items won’t shift the index of the remaining items you still have to check (which is the cause of your out of range exception).

    The first way causes a problem because you’re modifying the list while iterating over it. And that is, as you said, a big no-no.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to filter rows based on a column say c1 that contains boolean
I am trying to filter an XML based on multiple parameters that I have
I am trying to filter an listbox with text from a textbox, realTime. Here
I am trying to filter for an object that has a Title field, and
I'm trying to filter out some garbage text from a string with regex but
I'm trying to filter a set of records based on a sub-object's criteria. This
I am trying to filter any common words out of a string. I found
I'm trying to filter an XML file using XPath. The XPath that I'm using
I am trying to filter the jqGrid data by date range - that is,
I'm trying to filter results based on what a user selects in a select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.