473,378 Members | 1,346 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,378 software developers and data experts.

Removing items from FormCollection gives error

Hi! I implemeted Forms colection described in this link:

http://support.microsoft.com/default...en-us%3B815707

I need to loop through the collectionbase and based on form's tag either close the form or do something else.

foreach(Form myForm in Forms)
{
if(myForm.Tag != AppConfig.homePageTitle)
{
myForm.Close();
myForm.Dispose();
}
else
{
// Do something
}
}

I am getting the following error:

"Collection was modified; enumeration operation may not execute."

In good old VB I used a reversed loop. What can I do in C# CollectionBase ?

Many Thanks for your help,

-- Mike
Nov 16 '05 #1
2 5477
Hi Mike,

You cannot modify a collection while it's being enumerated. You might
try this :

Form formToDelete;

do
{
formToDelete = null;
foreach (Form myForm in Forms)
{
if (myForm.Tag != AppConfig.homePageTitle)
{
formToDelete = myForm ;
break;
}
}
if (formToDelete == null) break;
else
{
fc.Remove(formToDelete);
formToDelete.Close();
formToDelete.Dispose();
}
} while(true);

It has the disadvantage of iterating over the collection from the
start after each deletion, but I bet you don't have thousands of forms
in your collection. It should do the trick.

Michel

"Mike" <Mi**@discussions.microsoft.com> wrote in message news:<1F**********************************@microso ft.com>...
Hi! I implemeted Forms colection described in this link:

http://support.microsoft.com/default...en-us%3B815707

I need to loop through the collectionbase and based on form's tag either close the form or do something else.

foreach(Form myForm in Forms)
{
if(myForm.Tag != AppConfig.homePageTitle)
{
myForm.Close();
myForm.Dispose();
}
else
{
// Do something
}
}

I am getting the following error:

"Collection was modified; enumeration operation may not execute."

In good old VB I used a reversed loop. What can I do in C# CollectionBase ?

Many Thanks for your help,

-- Mike

Nov 16 '05 #2
For the sake of completion, here's what I've done:

In the FormCollection class I added the following to be able to address items by index:

public Form this [int index]
{
get
{
return ((Form) List[index]);
}
}
Then I could loop in reverse order:

Form myForm;
for (int i = Forms.Count-1; i>=0; i--)
{
myForm = Forms[i];

if(myForm.Tag != AppConfig.homePageTitle)
{
myForm.Close();
myForm.Dispose();
}
else
{
//do something
}
}

That did the trick.

Best,

--Mike

"Mike" wrote:
Hi! I implemeted Forms colection described in this link:

http://support.microsoft.com/default...en-us%3B815707

I need to loop through the collectionbase and based on form's tag either close the form or do something else.

foreach(Form myForm in Forms)
{
if(myForm.Tag != AppConfig.homePageTitle)
{
myForm.Close();
myForm.Dispose();
}
else
{
// Do something
}
}

I am getting the following error:

"Collection was modified; enumeration operation may not execute."

In good old VB I used a reversed loop. What can I do in C# CollectionBase ?

Many Thanks for your help,

-- Mike

Nov 16 '05 #3

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

Similar topics

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...
2
by: Chris | last post by:
I find tons of info on adding, but what about removing an item dynamically? I'm cycling through the list and removing users that have special rights. I've tried things like: ...
1
by: Alan Silver | last post by:
Hello, I have a page in which I'm trying to give the user the chance to manipulate a list of items. These are the price variations for a product, so each item consists of a name (eg, small,...
2
by: Curious | last post by:
Hi, I am removing problems but I am having a problem. The code below is the code being used to remove items from the listview. ListViewItem oldItem = listViewAllStates.SelectedItems;...
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: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
3
by: Stef Mientki | last post by:
hello, I want to remove some items from a dictionary, so I would expect this should work: Nets = {} ... fill the dictionary Nets for net in Nets: if net.upper() in Eagle_Power_Nets :
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.