473,758 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listBox1.Items. Remove help

I dont know what i am doing false.
the code :

private void button2_Click(o bject sender, System.EventArg s e)
{
foreach (string filename in listBox1.Select edItems)
listBox1.Items. Remove(filename );
}
gives me the error :

System.InvalidO perationExcepti on: The list that this enumerator is bound to
has been modified. An enumerator can only be used if the list doesn't
change.
at
System.Windows. Forms.EntryEnum erator.System.C ollections.IEnu merator.MoveNex t
()

Anyone can help?

Nov 16 '05 #1
4 6182
Bilo wrote:

I dont know what i am doing false.
the code :

private void button2_Click(o bject sender, System.EventArg s e)
{
foreach (string filename in listBox1.Select edItems)
listBox1.Items. Remove(filename );
}
gives me the error :

System.InvalidO perationExcepti on: The list that this enumerator is bound to
has been modified. An enumerator can only be used if the list doesn't
change.
at
System.Windows. Forms.EntryEnum erator.System.C ollections.IEnu merator.MoveNex t
()

Anyone can help?


You can't remove items from the listbox while you are iterating over that very
listbox. The problem is because it becomes difficult to maintain consistency
if items within the list are being removed.

What you need to do is to create an intermediate structure to hold the items to
be removed, then remove those items:

ArrayList files = new ArrayList();
foreach (string filename in listBox1.Select edItems)
files.Add(filen ame);
foreach (string filename in files)
listBox1.Items. Remove(filename );
Nov 16 '05 #2
"Bilo" wrote...
private void button2_Click(o bject sender, System.EventArg s e)
{
foreach (string filename in listBox1.Select edItems)
listBox1.Items. Remove(filename );
}
The error actually spells it out:
System.InvalidO perationExcepti on:
The list that this enumerator is bound to
has been modified. An enumerator can only
be used if the list doesn't change.


When you Remove an item from the ListBox, you also affect the "contents" of
its SelectedItems, which makes it impossible to continue the iteration, as
the iteration is *based* on it.

You could try to do something like this instead:

string[] sa = new string[listBox1.Select edItems.Count];
listBox1.Select edItems.CopyTo( sa,0);

foreach (string filename in sa)
{
listBox1.Items. Remove(filename );
}

// Bjorn A
Nov 16 '05 #3
Ahh first store the items.
Thx you.

"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> schrieb im Newsbeitrag
news:Ox******** ******@TK2MSFTN GP11.phx.gbl...
"Bilo" wrote...
private void button2_Click(o bject sender, System.EventArg s e)
{
foreach (string filename in listBox1.Select edItems)
listBox1.Items. Remove(filename );
}
The error actually spells it out:
System.InvalidO perationExcepti on:
The list that this enumerator is bound to
has been modified. An enumerator can only
be used if the list doesn't change.


When you Remove an item from the ListBox, you also affect the "contents"

of its SelectedItems, which makes it impossible to continue the iteration, as
the iteration is *based* on it.

You could try to do something like this instead:

string[] sa = new string[listBox1.Select edItems.Count];
listBox1.Select edItems.CopyTo( sa,0);

foreach (string filename in sa)
{
listBox1.Items. Remove(filename );
}

// Bjorn A

Nov 16 '05 #4
"Bilo" <ju********@yah oo.com> wrote in
news:#p******** ******@TK2MSFTN GP12.phx.gbl:
I dont know what i am doing false.
the code :

private void button2_Click(o bject sender, System.EventArg s e)
{
foreach (string filename in listBox1.Select edItems)
listBox1.Items. Remove(filename );
}
gives me the error :

System.InvalidO perationExcepti on: The list that this enumerator is
bound to has been modified. An enumerator can only be used if the list
doesn't change.
at
System.Windows. Forms.EntryEnum erator.System.C ollections.IEnu merator.Mov
eNext ()

Anyone can help?


to keep from duplicating all those strings, how bout this?
while (listBox1.Selec tedItems.Count > 0)
{
listBox1.Items. Remove(listBox1 .SelectedItems[0]);
}
Nov 16 '05 #5

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

Similar topics

0
630
by: stingrays | last post by:
Below is my code for a combobox. Basically, I have 2 comboboxes. 1 box is the application selection and the other retrieves data from the dataset (SQL Server) to populate the drop down in the otehr box. I want to make a selection in the app combobox, update the second box and when I select a new app, it should remove all items in teh 2nd combobox and refresh the list with only the selected app. With this code it appears the binding is...
3
3054
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is populated by the items in an arraylist shown in the code below. When the button is clicked I would also like the code to remove the items from the Arraylist. I've very little experience working with datagrids and arraylists so im finding this...
3
3115
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand list box. The problem is that if there are many items selected (thousands), then removing the items from the right side list box takes for ever because we are removing them one at a time, which causes the listbox to re-index everything before we...
0
5413
by: Frawls | last post by:
Hi, This is concerned with System.Web.UI.WebControls.DropDownList I am having problems creating a method which will remove list items from a preloaded dropdownlist. This dropdown is loaded when the page loads and the items are pulled from a database. I want to create a method which is called on the page load and will remove certain items from the dropdown list. I want the method to loop
4
11497
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you select, the first one in the listbox is always removed.(The listitem with an index of 0. Box is set to single selection mode) I've looked at multiple examples and they all do it this way. What's wrong? (variables are also being set to the values...
2
3254
by: Jan | last post by:
Hi there, I'm a newbee in vb.net with the following problem: 4444528; Baltic Birch; 16.20; 0 I have more of these lines in a ListBox1. Now I want to change the latest 0 with the value of a NumericUpDown1 (for ex. 8) My solution is this:
2
1342
by: Bernie Yaeger | last post by:
I want to open a window with a listbox (custlist) in it, but I want to automatically select a certain item in the listbox, and have it be the correct selectedindex. The call to open this window knows what it seeks, which is variable. So one time I may wish to select item 45 (marsdon) and sometimes item 1997 (albert). How can I do this? Thanks for any help. Bernie Yaeger
2
4881
by: Milsnips | last post by:
hi there, i have the following HTML code: -------------------------- <asp:DropDownList id="hddList" runat="server"> <asp:ListItem Value="1">Item 1</asp:ListItem> <asp:ListItem Value="2">Item 2</asp:ListItem> <asp:ListItem Value="3">Item 3</asp:ListItem> <asp:ListItem Value="4">Item 4</asp:ListItem> <asp:ListItem Value="5">Item 5</asp:ListItem>
5
2473
by: BlackBox | last post by:
I have a Listbox1 in the main form and I can add listbox1 items in the parent form. But when I close the parent form Listbox1 in the main form does not get refresh. I need to re-open the app. Any idea on how to refresh items in the main from from a parent form without opening the app. -Thanks
0
9492
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
10076
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9908
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...
1
9885
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
9740
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
8744
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...
1
7287
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...
1
3832
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
3
2702
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.