简体   繁体   中英

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. 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. When on the other hand I write a value into the ComboBox, the event does not fire. The event SelectionChangeCommitted does not fire in both cases. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM