80

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?

1
  • Not sure about on the desktop, but in .NET Compact, this event actually appears to fire before the Form_Load event, which is really problematic - nothing on the form is even set up yet when this code gets hit. On CF, the solution is unfortunately a form variable that gets set at the end of Load, and then the event handler checks for this variable before it fires. Commented Feb 11, 2013 at 2:19

7 Answers 7

157

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But what to do in case of autocompete?
@Arijit Mukherjee right. When typing a value into textbox instead of selecting directly. It won't trigger SelectionChangeCommitted event
Damn, I have been searching the web for quite a while to find a simple solution like this. I have been using the SelectionChanged event and it worked in VB.Net - WPF and now using it on C# - WinForms and it doesnt work or maybe it is just me. Thanks!
Akams Razor - the simplest solution is the most likely (and the best in this case).
15

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);

Comments

11

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!

2 Comments

Please add more info that elaborates your thinking.
Assigning a new DataSource fires a "selectedIndexChanged" event.
5

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.

4 Comments

booooooolean.. SelectionChangeCommitted much nicer
There is already a boolean flag ` Control.Created` telling you, that the form has finished loading, which is also valid for forms.
ToolStripComboBox variation does not have a SelectionChangeCommitted event. So, this method will have to do. Or, to prevent race conditions, use lock and Monitor.TryEnter.
@user1734987: That's not the same thing. There could be initialization code that needs to be completed and Control.Created won't fire that.
2

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

3 Comments

You need to add a SelectedValue != null check as well to this, and then it's a perfect solution.
Yes, this is going to be a good added check too.
myComboBox.Focused is usually False
1

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

Comments

-1

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

1 Comment

SelectionChangeCommitted will stop to call index change items, once all items loaded into dropbox then you can select one by one only, it will increase your loading speed also.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.