ComboBox Error Collection 已修改;枚举操作可能无法执行

ComboBox Error Collection was modified; enumeration operation may not exectue

我有一个组合框,里面每个值都有一个复选框。

当我更改 CustomerCBC.CheckBoxItems[1] 的 selectedIndex 时,他循环搜索 customerCBC.Items。

组合框中的最后一项需要更改时发生错误。

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

对于组合框,我使用 PresentationControls.CheckBoxComboBox 控件。

已经试过了'Lock the items in the combobox'

如果您需要更多信息,请随时询问。

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);

                }

            }

你不能

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

消除枚举的需要...

编辑: 您甚至可以合并并取消 if:

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