InvalidArgument =值'0'对'index'无效

时间:2013-06-12 04:58:42

标签: c# winforms combobox indexing invalid-argument

我在Windows窗体应用程序中有一个ComboBox,当我在ComboBox中输入文本时,我会动态地从字典中填充组合框但是我收到错误:

  

InvalidArgument ='0'的值对'index'无效。参数名称:index。

我写的代码如下:

private void comboBoxStateStateAdd_TextChanged(object sender, EventArgs e)
{
    if (!comboBoxStateStateAdd.Items.Contains(comboBoxStateStateAdd.Text))
    {
        if (!comboBoxStateStateAdd.DroppedDown)
            comboBoxStateStateAdd.DroppedDown = true;
        foreach (KeyValuePair<int, string> pair in stateList)
        {
            if (pair.Value.ToUpper().Contains(comboBoxStateStateAdd.Text.ToUpper()))
            {
                if (!comboBoxStateStateAdd.Items.Contains(pair.Value))
                    comboBoxStateStateAdd.Items.Add(pair.Value);
            }
            if (!pair.Value.ToUpper().Contains(comboBoxStateStateAdd.Text.ToUpper()))
            {
                if (comboBoxStateStateAdd.Items.Contains(pair.Value))
                    comboBoxStateStateAdd.Items.Remove(pair.Value);
            }
        }
     }
}

0 个答案:

没有答案