この方法でコンボボックスを「読み取り専用」にしています:
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 になることはありません。フォーム上に複数の ComboBox があります...???