c# checkedlistbox setting items as checked and unchecking during foreach item in checkbox

i have a checked listbox
and a function that uses the checked item for work (it references a file on the drive)
what i am trying to do it upon sucessful action uncheck it.
if it fails leave it checked and move on to the next one.

here is what i have. but i get an error on this line "foreach (var item in cb.CheckedItems)"
saying :

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

so what can i do to this code to make it work??

thanks

:)


foreach (var item in cb.CheckedItems)
            {
                //set item var
                int cbList = 0;
                System.Diagnostics.Debug.Assert(item is FileMoniker);
                FileMoniker f = (FileMoniker)item;
                BatchWizardData data = BatchWizardData.Load(f.FileInfo.FullName);
                LogicFactory factory = new LogicFactory();
                foreach(var step in data.Steps)
                {
                    if (step.IsValid)
                    {
                        var scope = Scope.GlobalScope;
                        using (var helper = factory.CreateCommitHelper(scope,
                             step, this.managedBrowser1, currentNameDataSource, null, true, step.Step))
                        {
                            while (!helper.Spin())
                                scope.DoEvents();
                            Exception errMessage = helper.Result;
                            if (errMessage != null)
                            {
                                //FormUtils.ShowEx(this, errMessage, "Error on committing");
                                failedLog();
                                cbList++;
                            }
                            else
                            {
                                if ((this.managedBrowser1.Contains("everything ok")) ||
                                    (this.managedBrowser1.Contains("well done")))
                                {
                                    successLog();
                                    //if successful uncheck the item, so that it can be tried again
                                    checkedListBox8.SetItemChecked(cbList, false);
                                    cbList++;
                                }

                                else
                                {
                                    failedLog();
                                    cbList++;
                                    
                                }
                                //MessageBox.Show("OK");
                            }
                        }
                        
                    }
                }
                
                     
                
                
            }
        }

Open in new window

nikon70Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
C# does not permit one to modify the iteration variable in a foreach loop. If you need to do modifications, use a for loop and index the collection.
SriVaddadiCommented:
You cannot un-check an item from the checked item list as you are iterating through it.  You will have to do it two steps.  In the first step capture the indexes of the items to be un-checked. In the second step, uncheck them iterating through the indexes (stored in arrary or collection in the first step)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nikon70Author Commented:
thank you i will have to work that one out.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.