[英]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
I have a ComboBox with an integer list as its data source. 我有一个带有整数列表的ComboBox作为其数据源。 The list contains integers from 0 up to 255. When I select a value by means of the mouse from the drop-down list, the event fires. 列表包含从0到255的整数。当我通过鼠标从下拉列表中选择一个值时,事件触发。 When on the other hand I write a value into the ComboBox, the event does not fire. 另一方面,当我将一个值写入ComboBox时,该事件不会触发。 The event SelectionChangeCommitted does not fire in both cases. 在两种情况下均不会触发事件SelectionChangeCommitted。 Please I need an explanation. 请给我一个解释。 Thank you in advance. 先感谢您。
You can create this event using the code snippet below: 您可以使用以下代码段创建此事件:
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");
}
I defined the event form inside this example, you can use it in a separate class. 我在此示例中定义了事件表单,您可以在单独的类中使用它。 Good Luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.