0

I have a comboBox with a checkbox inside for each value.

When i change the selectedIndex of CustomerCBC.CheckBoxItems[1], he loop trought the customerCBC.Items.

The error occurs when the last item in the comboBox need to change.

Error:Collection was modified; enumeration operation may not execute.

For the comboBox i use the PresentationControls.CheckBoxComboBox control.

Already tried to 'Lock the items in the combobox'

If you need more info, feel free to ask.

private void CheckComboBox_SelectedIndexChanged(object sender, EventArgs e)
    { 
        int countChecked = 2;
        if (customerCBC.Items.Count > 2)
        {
            if (customerCBC.CheckBoxItems[1].Checked == true)
            {

                foreach (Object dr in customerCBC.Items)
                {
                    if (customerCBC.Items.IndexOf(dr) > 1){
                    //Set all the customers checked
                    customerCBC.CheckBoxItems[countChecked].Checked = true;
                    countChecked++;
                    }
                }
            }
            else
            {
                try
                {
                    foreach (Object dr in customerCBC.Items)
                    {
                        if (customerCBC.Items.IndexOf(dr) > 1)
                        {
                            //Set all the customers unchecked
                            customerCBC.CheckBoxItems[countChecked].Checked = false;
                            // MessageBox.Show(countChecked.ToString());
                            countChecked++;
                        }
                    }
                }
                catch (Exception e1)
                {
                    //MessageBox.Show(countChecked.ToString());
                    MessageBox.Show(e1.Message);

                }

            }
JP..t
  • 575
  • 1
  • 6
  • 28
  • Related: http://stackoverflow.com/q/604831 – Soner Gönül Apr 30 '15 at 10:27
  • I can't create a list from the items in the checkBox... – JP..t Apr 30 '15 at 10:30
  • What kind of `ComboBox` is that? A regular one has not a `CheckBoxItems` property. – Tim Schmelter Apr 30 '15 at 10:31
  • @TimSchmelter I created the comboBox with the PresentationControls.CheckBoxComboBox reference – JP..t Apr 30 '15 at 11:16
  • You are iterating through the combobox items and modifing them too in the iteration..That is what causing the pbm i think.!! – Vishal Apr 30 '15 at 11:22
  • @JP..t: i remember that i've also used that control for a project. But i'm not sure what you are trying to achieve here. In general you cannot modify the collection that you are enumerating in a `foreach`. Maybe you have to store the items that you want to modify in a separate collection. Then loop that afterwards and check them. – Tim Schmelter Apr 30 '15 at 11:25
  • Modifying the item should be quite fine - as long as you do not modify the collection (insert/delete/move). – Mario The Spoon Apr 30 '15 at 11:31

1 Answers1

0

Couldn't you

for( int i = 2; i < customerCBC.Items.Count; ++i )
{
   customerCCBC.CheckBoxItems[i].Checked = false;
}

Doing away with the need for an enumeration...

Edit: You could even combine and do away with the if:

for( int i = 2; i < customerCBC.Items.Count; ++i )
{
   customerCCBC.CheckBoxItems[i].Checked = customerCBC.CheckBoxItems[1].Checked;
}
Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36