473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem looping through listbox to delete item

Here's the code:
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items
If li.Selected = True Then
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next

When I remove an item I get this error:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute. at
System.Collections.ArrayListEnumeratorSimple.MoveN ext()

I think its because I'm changing the collection while trying to loop through
it.

Any ideas for a workaround?

Thanks.
Nov 18 '05 #1
3 2673
That's correct. You cannot change a collection while you are looping
through it.

However, a simple change can allow you to do this:

dim alItemsToDelete as ArrayList = new ArrayList()
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items
If li.Selected = True Then
alItemsToDelete.Add(li)
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next
For Each li as ListItem In alItemsToDelete
Me.lstAssignedEmailAddresses.Items.Remove(li)
Next

Hope this helps

Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Here's the code:
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items
If li.Selected = True Then
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next

When I remove an item I get this error:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute. at
System.Collections.ArrayListEnumeratorSimple.MoveN ext()

I think its because I'm changing the collection while trying to loop through it.

Any ideas for a workaround?

Thanks.

Nov 18 '05 #2
Thanks!
"Ben Lucas" <be*@nospam.solien.nospam.com> wrote in message
news:YI********************@comcast.com...
That's correct. You cannot change a collection while you are looping
through it.

However, a simple change can allow you to do this:

dim alItemsToDelete as ArrayList = new ArrayList()
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items
If li.Selected = True Then
alItemsToDelete.Add(li)
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next
For Each li as ListItem In alItemsToDelete
Me.lstAssignedEmailAddresses.Items.Remove(li)
Next

Hope this helps

Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Here's the code:
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items If li.Selected = True Then
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next

When I remove an item I get this error:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at
System.Collections.ArrayListEnumeratorSimple.MoveN ext()

I think its because I'm changing the collection while trying to loop

through
it.

Any ideas for a workaround?

Thanks.


Nov 18 '05 #3
As someone else said, you can't modify a collection while looping through
it.

However, if you don't use an enumerator, but just use a regular for loop,
that will work fine.

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Here's the code:
For Each li As ListItem In Me.lstAssignedEmailAddresses.Items
If li.Selected = True Then
Me.lstAssignedEmailAddresses.Items.Remove(li)
End If
Next

When I remove an item I get this error:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute. at
System.Collections.ArrayListEnumeratorSimple.MoveN ext()

I think its because I'm changing the collection while trying to loop through it.

Any ideas for a workaround?

Thanks.

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I can't figure this out. I want the ability to delete an item in a listbox and leave the window area blank, like it is when the listbox is first created and nothing selected. Right...
5
by: Andrew | last post by:
Hi I just started learning wxPython I wanted to know how I could do this in wxPython self.listbox.delete(0, END) for item in self.results: self.listbox.insert(END, item)
4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
1
by: Josema | last post by:
Hi to all, I have a class (persons) that derives from collection base: and another class (person) with this properties: -ID -Name When i have complete filled the object Persons with all...
2
by: Josema | last post by:
Hi, i have a windows application. This application uses a object that its of type collectionbase. For other hand i have a ListBox, and i use this collection to fill it. I have implemented...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
1
by: Marcos Ribeiro | last post by:
I Have a ListBox that uses an ArrayList with objects as DataSource The objects are added to the ArrayList using a Database If I Add/Delete any item on the ListBox, the ArrayList is rebuilt with...
1
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.