这就是我所做的:
创建了一个组合框列表:
List<ComboBox> toolParameterComboBoxes = new List<ComboBox>();
将所有组合框添加到该列表中:
toolParameterComboBoxes.Add(countcomboBox);(重复...)
更改了所有组合框的 selectionIndexChanged 事件以使用它:
private void validateComboBox(object sender, EventArgs e)
{
ComboBox thisCB = sender as ComboBox;
if (thisCB.Text != "")
{
foreach (ComboBox cb in toolParameterComboBoxes)
{
if (thisCB.Name != cb.Name && thisCB.Text == cb.Text && thisCB.Text != "" && cb.Text != "")
{
MessageBox.Show("You cannot duplicate tool parameters." + "\r\n" + "\r\n"
+ "That option has been selected in " + cb.Name.Replace("comboBox", ""), "Error");
thisCB.SelectedIndex = 0;
break;
}
}
}
}
这处理空的组合框。由于某种原因thisCB.Text = "";不起作用,所以我在他们的每个集合中添加了一个空的组合框项目,并简单地将选定的索引更改为 0,因此永远无法选择重复的项目。