473,485 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

listBox1.Items.Remove help

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

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

System.InvalidOperationException: 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.EntryEnumerator.System.Collec tions.IEnumerator.MoveNext
()

Anyone can help?

Nov 16 '05 #1
4 6161
Bilo wrote:

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

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

System.InvalidOperationException: 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.EntryEnumerator.System.Collec tions.IEnumerator.MoveNext
()

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.SelectedItems)
files.Add(filename);
foreach (string filename in files)
listBox1.Items.Remove(filename);
Nov 16 '05 #2
"Bilo" wrote...
private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
The error actually spells it out:
System.InvalidOperationException:
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.SelectedItems.Count];
listBox1.SelectedItems.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**********@DoNotSpam.hotmail.com> schrieb im Newsbeitrag
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"Bilo" wrote...
private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
The error actually spells it out:
System.InvalidOperationException:
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.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(sa,0);

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

// Bjorn A

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

private void button2_Click(object sender, System.EventArgs e)
{
foreach (string filename in listBox1.SelectedItems)
listBox1.Items.Remove(filename);
}
gives me the error :

System.InvalidOperationException: 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.EntryEnumerator.System.Collec tions.IEnumerator.Mov
eNext ()

Anyone can help?


to keep from duplicating all those strings, how bout this?
while (listBox1.SelectedItems.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...
3
3019
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...
3
3104
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...
0
5397
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...
4
11473
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...
2
3235
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...
2
1329
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...
2
4864
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...
5
2445
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. ...
0
7090
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,...
0
6960
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
7161
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...
0
7275
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...
1
4857
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
4551
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
3058
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
1376
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 ...
0
247
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.