![](/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.