How do I iterate through a listbox without using FOR EACH

Discussion in 'ASP .Net' started by COHENMARVIN, Sep 29, 2005.

  1. COHENMARVIN

    COHENMARVIN Guest

    I've been doing a simple for-each loop to remove multiple selected
    items from one listbox, and to dump them in another listbox.

    for each item in LeftListBox.Items
    if (item.Selected = true) Then
    RightListBox.Items.Add(item)
    LeftListBox.Items.Remove(item)
    End If
    Next
    The problem is that this code doesn't work because its modifying the
    collection in the left listbox. I get an error such as:
    "Collection was modified; enumeration operation may not execute".
    I searched the internet for an explanation, and I see one person who
    says that you cannot use For-each in a situation like this, instead you
    should use For-Next. My question: How do you iterate through a listbox
    with for-next?
    Thanks,
    Marvin
    COHENMARVIN, Sep 29, 2005
    #1
    1. Advertising

  2. How about something like

    for( int i=0; i<LeftListBox.Items.Count; i++ )
    {
    ...
    }

    "COHENMARVIN" <> wrote in message
    news:...
    > I've been doing a simple for-each loop to remove multiple selected
    > items from one listbox, and to dump them in another listbox.
    >
    > for each item in LeftListBox.Items
    > if (item.Selected = true) Then
    > RightListBox.Items.Add(item)
    > LeftListBox.Items.Remove(item)
    > End If
    > Next
    > The problem is that this code doesn't work because its modifying the
    > collection in the left listbox. I get an error such as:
    > "Collection was modified; enumeration operation may not execute".
    > I searched the internet for an explanation, and I see one person who
    > says that you cannot use For-each in a situation like this, instead you
    > should use For-Next. My question: How do you iterate through a listbox
    > with for-next?
    > Thanks,
    > Marvin
    >
    Peter Rilling, Sep 29, 2005
    #2
    1. Advertising

  3. COHENMARVIN

    Ben Amada Guest

    COHENMARVIN wrote:

    > I've been doing a simple for-each loop to remove multiple selected
    > items from one listbox, and to dump them in another listbox.
    >
    > for each item in LeftListBox.Items
    > if (item.Selected = true) Then
    > RightListBox.Items.Add(item)
    > LeftListBox.Items.Remove(item)
    > End If
    > Next
    > The problem is that this code doesn't work because its modifying the
    > collection in the left listbox. I get an error such as:
    > "Collection was modified; enumeration operation may not execute".
    > I searched the internet for an explanation, and I see one person who
    > says that you cannot use For-each in a situation like this, instead you
    > should use For-Next. My question: How do you iterate through a listbox
    > with for-next?
    > Thanks,
    > Marvin


    The code below should work using a While loop. Warning: Untested, but
    should get you closer to what you're trying to do:

    Dim blnContinue As Boolean = True
    Dim lngIndex As Long

    While blnContinue

    If LeftListBox.Items(lngIndex).Selected Then
    RightListBox.Items.Add(LeftListBox.Items(lngIndex))
    LeftListBox.Items.Remove(LeftListBox.Items(lngIndex))
    Else
    lngIndex += 1
    End If

    blnContinue = (lngIndex < LeftListBox.Items.Count)

    End While

    Also, I don't remember offhand if the Items collection of the ListBox is
    zero-based or not. The above example assumes it's zero-based.

    Ben
    Ben Amada, Sep 29, 2005
    #3
  4. COHENMARVIN

    nzolotar

    Joined:
    May 23, 2008
    Messages:
    2
    Location:
    Tamarac, FL
    another way to do it

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
    'check if anything selected
    Dim removeOK As Boolean = False
    For Each lit As ListItem In lbAllViolations.Items
    If lit.Selected Then removeOK = True
    Next
    If removeOK Then
    Dim collectionSize As Integer = lbCurrentViolations.Items.Count - 1
    Dim i As Integer = 0
    Do While i <= collectionSize
    If lbCurrentViolations.Items.Item(i).Selected Then
    lbCurrentViolations.Items.Remove(lbCurrentViolations.Items.Item(i))
    collectionSize -= 1
    Else
    i = i + 1
    End If
    Loop
    End If
    End Sub
    Last edited: May 23, 2008
    nzolotar, May 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,082
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,437
    runescience
    Feb 9, 2006
  3. Tom_chicollegeboy

    how to iterate through each set

    Tom_chicollegeboy, Nov 4, 2007, in forum: Python
    Replies:
    8
    Views:
    516
    Dennis Lee Bieber
    Nov 4, 2007
  4. John
    Replies:
    4
    Views:
    892
    RedGrittyBrick
    Apr 1, 2008
  5. ixnay

    Using Each to Iterate

    ixnay, Oct 3, 2006, in forum: Ruby
    Replies:
    9
    Views:
    134
    Mariano Kamp
    Oct 4, 2006
Loading...

Share This Page