![](/img/trans.png)
[英]ComboBox.SelectedIndexChanged does not fire when programatically changing the index
[英]SelectedIndexChanged event does not fire when I write a value into the ComboBox. It does fire when I select a value from the comboBox.Items list
我有一个带有整数列表的ComboBox作为其数据源。 列表包含从0到255的整数。当我通过鼠标从下拉列表中选择一个值时,事件触发。 另一方面,当我将一个值写入ComboBox时,该事件不会触发。 在两种情况下均不会触发事件SelectionChangeCommitted。 请给我一个解释。 先感谢您。
您可以使用以下代码段创建此事件:
public event IndexChangeHandler IndexChanged;
public delegate void IndexChangeHandler (object s);
private void button4_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
int temp = 0;
while (true)
{
System.Threading.Thread.Sleep(2000);
comboBox1.BeginInvoke((MethodInvoker)delegate
{
if (comboBox1.Items.Count != temp)
{
IndexChanged(this);
temp= comboBox1.Items.Count;
}
});
}
});
IndexChanged += Form1_IndexChanged;
for (int i = 0; i < 5; i++)
{
comboBox1.Items.Add(i);
}
}
private void Form1_IndexChanged(object s)
{
MessageBox.Show("Test");
}
我在此示例中定义了事件表单,您可以在单独的类中使用它。 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.