在C#中以编程方式触发comboBox SelectedIndexChanged事件

时间:2015-12-20 12:19:14

标签: c# events combobox selectedindex selectedindexchanged

win表格上有一个组合框和一个按钮。如何通过单击按钮来激活comboBox selectedIndexChanged。

1 个答案:

答案 0 :(得分:5)

您应该重新考虑您的代码设计。看来你想提升事件来间接触发一些动作。为什么不这样试试呢:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     // do the things that should happen only if a real change happend
     // ...
     // then do the special thing you want
     DoTheOtherStuff();
}
private void button1_Clicked(object sender, EventArgs e)
{
    // do the things to happen when the button is clicked
    // ...
    // then do the special thing you want
    DoTheOtherStuff();
}
private void DoTheOtherStuff()
{
    // the special thing you want
}

编辑:如果您的遗留代码与您的评论建议一样尴尬,您仍然可以使用这种尴尬的方式:

private void button1_Clicked(object sender, EventArgs e)
{
    comboBox1_SelectedIndexChanged(comboBox1, EventArgs.Empty);
}