Stop comboBox's selectedIndexChanged event from firing when the form loads

Viewed 89680

I have a form with a ComboBox that provides a dropdownlist. On the comboBox's SelectedIndexChanged event, am running some code, but I don't want that code to run when the form loads. Unfortunately, when I load the form (before I make a selection in the combobox), SelectedIndexChanged of the combobox fires (I think when the combobox is databinding). Is there a way of avoiding such behaviour?

7 Answers

Here is a simple solution that leaves your code almost untouched:

In the SelectedIndexChanged event, check if the myComboBox handle is created using the (IsHandleCreated) method. Another added check is to check if the user is actually focusing your combobox control to change selected index.

 private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (myComboBox.IsHandleCreated &&  myComboBox.Focused)
        {
           // Do something here
        }
    }
Related