简体   繁体   中英

Combobox not firing SelectedIndexChanged when programmatically selecting index 0 (from -1)

I have a combobox for wich I am using the SelectIndexChanged event to capture both user and programmatically changes.

Clearing and reloading the list bound to the combobox will fire the eventhandler with index -1 naturally.

But then with selectedindex=-1

combobox1.SelectedIndex = 0 ; // will NOT fire the event.

but

combobox1.SelectedIndex = 1 ; // or higher number WILL fire the event.

In both cases I AM programmatically changing the selextedindex and expect the event to be fired.

I verified the behavior in a simple form.

namespace cmbTest
{
    public partial class Form1 : Form
    {
        private BindingList<string> items = new BindingList<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = items;
            loadItems();
        }

        private void loadItems()
        {
            items.Add("chair");
            items.Add("table");
            items.Add("coffemug");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Fired with selected item index:" + comboBox1.SelectedIndex);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int index = comboBox1.SelectedIndex;

            // Clear and reload items for whatever reason.
            items.Clear();
            loadItems();

            // try select old item index; if it doesnt exists, leave it.
            try { comboBox1.SelectedIndex = index; }
            catch { }
        }





    }
}

The form has a combobox1 and a button1.

EDIT for clarity (I hope):

  1. Run program
  2. Select 'chair'. Message "Fired with selected item index:0"
  3. Hit button. Message "Fired with selected item index:-1"
  4. Select 'table'. Message "Fired with selected item index:1"
  5. Hit button. Messages "Fired with selected item index:-1" AND "Fired with selected item index:1".

I expect to get two messages when hitting button when "chair" is selected too, since I programmatically changes the index to 0.

So, why is this not working as I expect it to do, and what will be an acceptable workaround?

When your first item is added to the items collection the index is automatically changed to 0. That's why when your previous index is saved as 0, and then you set it again using this line comboBox1.SelectedIndex = index; , the index is not changed. That's why the event is not fired.

Looking at the source code for the ComboBox, the event is not fired in 2 cases : Either an expcetion is thrown, or the index is set to the same value that it is was.

If you really want some hack around this, you can do it this way:

int index = comboBox1.SelectedIndex;

// Clear and reload items for whatever reason.
items.Clear();
loadItems();

if(comboBox1.SelectedIndex == index)
{
    comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
    comboBox1.SelectedIndex = -1;
    comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}

// try select old item index; if it doesnt exists, leave it.
try { comboBox1.SelectedIndex = index; }
catch
{
}

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