我在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);
}
}
}
}