How do I iterate through a listbox without using FOR EACH

C

COHENMARVIN

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
 
P

Peter Rilling

How about something like

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

Ben Amada

COHENMARVIN said:
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
 
Joined
May 23, 2008
Messages
2
Reaction score
0
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:

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top