472,784 Members | 909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 6102
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
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
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
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
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
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
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
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
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
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.