473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem looping through listbox to delete item

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

When I remove an item I get this error:
System.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute. at
System.Collecti ons.ArrayListEn umeratorSimple. MoveNext()

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 2697
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.lstAssignedE mailAddresses.I tems
If li.Selected = True Then
alItemsToDelete .Add(li)
Me.lstAssignedE mailAddresses.I tems.Remove(li)
End If
Next
For Each li as ListItem In alItemsToDelete
Me.lstAssignedE mailAddresses.I tems.Remove(li)
Next

Hope this helps

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

When I remove an item I get this error:
System.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute. at
System.Collecti ons.ArrayListEn umeratorSimple. MoveNext()

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.sol ien.nospam.com> wrote in message
news:YI******** ************@co mcast.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.lstAssignedE mailAddresses.I tems
If li.Selected = True Then
alItemsToDelete .Add(li)
Me.lstAssignedE mailAddresses.I tems.Remove(li)
End If
Next
For Each li as ListItem In alItemsToDelete
Me.lstAssignedE mailAddresses.I tems.Remove(li)
Next

Hope this helps

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

When I remove an item I get this error:
System.InvalidO perationExcepti on: Collection was modified; enumeration operation may not execute. at
System.Collecti ons.ArrayListEn umeratorSimple. MoveNext()

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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Here's the code:
For Each li As ListItem In Me.lstAssignedE mailAddresses.I tems
If li.Selected = True Then
Me.lstAssignedE mailAddresses.I tems.Remove(li)
End If
Next

When I remove an item I get this error:
System.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute. at
System.Collecti ons.ArrayListEn umeratorSimple. MoveNext()

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
2481
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 now the following is happening. If I simply delete the item in the listbox, that item will stay in the view window until another item is selected. Or I can select an item in the listbox as active and then view it, but I don't want to do that.
5
10620
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
22616
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 text of the selected item ? Pete
1
3212
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 the persons, i put each persons inside a ListBox following this:
2
1256
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 the methods (in my class that derives from collection base):
1
7901
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 last item:- Specified argument was out of the range of valid values. Parameter name: '64' is not a valid value for 'Value'. The 64 reduces depending on how many listbox items are displayed.
1
2244
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 something like: MyArrList.Clear() MyListBox.DataSource = nothing ...... peek database and ReFill MyArrList with MyObjects If MyArrList.Count > 0 Then MyListBox.DataSource = MyArrList MyListBox.ValueMember = "SomeProperty1"
1
2883
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 is added to that column/row. When the admin clicks "update" I am unable to retrieve the values that were selected from that listbox. Basically, This is a multi-select listbox, so I need to loop to pull all selected items. This is the only...
1
4028
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
8840
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...
0
8730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9064
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...
1
6669
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
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
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
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
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.