473,597 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ 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.Ite ms
if (item.Selected = true) Then
RightListBox.It ems.Add(item)
LeftListBox.Ite ms.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 2025
How about something like

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

"COHENMARVI N" <co*********@ho tmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.Ite ms
if (item.Selected = true) Then
RightListBox.It ems.Add(item)
LeftListBox.Ite ms.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.Ite ms
if (item.Selected = true) Then
RightListBox.It ems.Add(item)
LeftListBox.Ite ms.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.Ite ms(lngIndex).Se lected Then
RightListBox.It ems.Add(LeftLis tBox.Items(lngI ndex))
LeftListBox.Ite ms.Remove(LeftL istBox.Items(ln gIndex))
Else
lngIndex += 1
End If

blnContinue = (lngIndex < LeftListBox.Ite ms.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
14025
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 than the listbox size. Please refer to this link to get the idea of what I would like to do. Unfortunately this one is for VB.
8
8061
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 it with a table and within the table a yes/no field would determine which box it would be in. An Update statement would be fired changing it from yes to no or vice versa and then refreshing the listboxes. This doesn't seem to scale very well as...
2
5099
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 String For Each itm In ListBox.Items ' what goes here to output the display text for each item in the
2
4440
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
3640
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 In FileInfo If Thing.Displayed <> True Then GoTo Iterate 'skip this entry; try next one End If
4
2788
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 keypress? For example: A have a listbox with a number of selections. The up and down keyboard keys change the indicated listbox selection and by default the <tabkey takes the focus away from the listbox and onto a button that will submit the selection. I'm...
1
2077
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) determining what shows up in listbox2 (multi_select), which determines listbox3, etc.... Each each listbox is populated from a different table in the same database. The databinding examples given in the video seem to leave no way for a
4
4960
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 listed first followed by the files.
0
2674
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. The majority of my items contain much more than 10 characters and thus the reason for my use of owner drawn listBoxes - I do not want to use a horizontal scrollbar, instead I want the text of each item to wrap onto multiple lines. I do use a...
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5431
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
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 we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.