我用这种方式制作一个"只读"组合框:
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;表单上有几个组合框。。。???
组合框很可能在其他面板中。
尝试递归地遍历它们:
private void button1_Click(object sender, EventArgs e) {
ChangeCombos(this);
}
private void ChangeCombos(Control parent) {
foreach (Control c in parent.Controls) {
if (c.Controls.Count > 0) {
ChangeCombos(c);
} else if (c is ComboBox) {
(c as ComboBox).SelectedValueChanged += comboBox1_SelectedValueChanged;
}
}
}
步骤,尽管它在开始处设置了断点,并调用c.gettype()
你也可能想做这个
if( c.gettype() == typeof(ComboBox))
{
}