简体   繁体   English

当我将值写入ComboBox时,不会触发SelectedIndexChanged事件。 当我从comboBox.Items列表中选择一个值时,它会触发

[英]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.

相关问题 以编程方式更改索引时,不会触发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged does not fire when programatically changing the index 以编程方式填充ComboBox时不会触发任何事件 - No events fire when I populate ComboBox programmatically 无法为组合框中的单个项目触发SelectedIndexChanged事件 - Unable to fire SelectedIndexChanged Event for a single item in a combobox 使用WinForms c#从ComboBox.Items [i]中获取ValueMember - ValueMember from ComboBox.Items[i] using WinForms c# 为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件? - Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified? 为什么ComboBox.SelectedIndexChanged事件在加载表单之前触发三次? - Why does ComboBox.SelectedIndexChanged event fire three times before form load? Fire comboBox SelectedIndexChanged仅来自用户点击 - Fire comboBox SelectedIndexChanged only from user click ListBox中的ComboBox不会触发SelectionChanged事件 - ComboBox in a ListBox does not fire SelectionChanged event 当我设置其他组合框的数据集时,`SelectedIndexChanged`将触发事件 - `SelectedIndexChanged` fires event when I set dataset of a different combobox ComboBox 的事件 comboBox_TextChanged 并不总是触发 - The event comboBox_TextChanged of the ComboBox does not always fire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM