473,479 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I iterate through a listbox without using FOR EACH

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
2 2016
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14014
by: hiroshi ochi | last post by:
Hello, Using MSIE 6.0 and above, with javascript is it possible to display an individual tooltip for each item in a listbox? I need this functionality to show the listitems that are longer...
8
8053
by: Bill | last post by:
I'm trying to create a wizardlike interface using a couple listboxes. I know you've seen it before. You double click on an item in one listbox and it "moves" it to the other. I used to approach...
2
5092
by: Daryll SHatz | last post by:
What object is returned by a bound (to a DataSet) ListBox when it is iterated? Listbox.DataSource = DataSet1.MyTable ListBox.DisplayMember = "Col1" ListBox.ValueMember = "Col2" Dim s as...
2
4432
by: Brent Burkart | last post by:
How do I iterate through listbox to pull out the data? I am drawing a blank. Thanks, Brent
6
3632
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI...
4
2781
by: Jeff | last post by:
....still new at this. ...hopefully a simple question Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on each...
1
2068
by: lance2001 | last post by:
Hi, After viewing the entire Visual Basic 2005 Express Edition for Beginners video series, I have begun building a database driven application that will make use of listbox1 (multi-select)...
4
4949
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
0
2662
by: LostInMd | last post by:
Hi All, I've got an owner drawn listBox where I draw and measure the items that I add to the listBox. For example, I have a listBox that can only display 10 characters on each horizontal line. ...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.