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

WinformsData BindingCombobox

Winforms Problem Overview


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?

Winforms Solutions


Solution 1 - Winforms

If you want to react only when the user change the selected item in the combo box, then it is better to subscribe to SelectionChangeCommitted.

Solution 2 - Winforms

You can simply unbind the SelectedIndexChanged event, call your fill function and bind the SelectedIndexChanged event again. Unfortunately, this doesn't work with a grid.

For example:

this.cmb.SelectionChanged -= new System.EventHandler(this.cmb_SelectionChanged);
cmb.fill(); //Your function
this.cmb.SelectionChanged += new System.EventHandler(this.cmb_SelectionChanged);

Solution 3 - Winforms

Be sure to set the DataSource property in your onload() function after assigning the ValueMember and Datamember properties.

This will help you to solve your problem!

Solution 4 - Winforms

Why not have a boolean flag that indicates when your Form has finished loading?

In your SelectionChanged event, check if the boolean flag is true. If it is true then handle the event, otherwise ignore it.

Solution 5 - Winforms

VB

RemoveHandler lbxNomes.SelectedIndexChanged, AddressOf lbxNomes_SelectedIndexChanged
lbxNomes.DataSource = dst
Label1.Text = String.Format("Encontrados {0} Sócios nesta pesquisa", dst.Rows.Count)
Label1.Visible = True
AddHandler lbxNomes.SelectedIndexChanged, AddressOf lbxNomes_SelectedIndexChanged

Solution 6 - Winforms

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
        }
    }

Solution 7 - Winforms

It worked for me in a way with the following code:

  private void ddlChapter_SelectionChangeCommitted(object sender, EventArgs e)
    {
        if (ddlChapter.SelectedValue != null)
        {
           // Do something here
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStackTraceView Question on Stackoverflow
Solution 1 - WinformsarbiterView Answer on Stackoverflow
Solution 2 - WinformsShantanu GuptaView Answer on Stackoverflow
Solution 3 - WinformsRanjithView Answer on Stackoverflow
Solution 4 - Winformsuser195488View Answer on Stackoverflow
Solution 5 - WinformsPaulos02View Answer on Stackoverflow
Solution 6 - WinformsMohamed NagiebView Answer on Stackoverflow
Solution 7 - WinformsAnjan KantView Answer on Stackoverflow