我正在以这种方式制作一个“只读”组合框:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
// for this to work, set the comboboxes' Tag to its SelectedIndex after setting that
ComboBox cb = sender as ComboBox;
int validSelection = Convert.ToInt32(cb.Tag);
if (cb.SelectedIndex != validSelection )
{
cb.SelectedIndex = validSelection;
}
}
...然后尝试将表单上的所有组合框设置为该处理程序,如下所示:
foreach (Control c in this.Controls)
{
if (c is ComboBox)
{
(c as ComboBox).SelectedValueChanged += comboBox1_SelectedValueChanged;
}
}
...但是 if 条件永远不等于 true;表单上有几个 ComboBoxes ......???