By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

How do I iterate through a listbox without using FOR EACH

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
How about something like

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

"COHENMARVIN" <co*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 19 '05 #2

P: n/a
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(lngInde x))
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
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.