在 C# 中以编程方式触发 comboBox SelectedIndexChanged 事件
Fire a comboBox SelectedIndexChanged event programmatically in C#
win 窗体上有一个组合框和一个按钮。如何通过单击按钮触发 comboBox selectedIndexChanged。
您应该重新考虑一下您的代码设计。似乎您想引发事件以间接触发某些操作。为什么不试试这样:
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);
}
win 窗体上有一个组合框和一个按钮。如何通过单击按钮触发 comboBox selectedIndexChanged。
您应该重新考虑一下您的代码设计。似乎您想引发事件以间接触发某些操作。为什么不试试这样:
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);
}