Tikam Sangwani
Tikam Sangwani

Reputation: 325

Combobox selected index changed event not fired in code

I am working on a Windows Forms application and I have a combobox named cmbCountry. I am binding this combobox to a list which contains names of countries. Following is the code to populate the combobox.

cmbCountry.DataSource = lstcountry;

Next I want to set selected item as "United States of America". so I added the following code

cmbCountry.SelectedItem="United States of America";

I want to do some code on selection change event of this combobox.

private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
    \\some code
}

This method is suppose to be call when I set the selected item. But it is not getting called. However when I select "United States of America" from UI part(Design Part) this event getting called. I want to get called this event when I set the selected item.

Upvotes: 2

Views: 10091

Answers (2)

Szymon
Szymon

Reputation: 43023

Change your event to SelectedIndexChanged:

private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        \\some code
    }

And change the event handler (which may be automatically generated):

this.cmbCountry.SelectedIndexChanged += new System.EventHandler(this.lstResults_SelectedIndexChanged);

Upvotes: 0

pinkfloydx33
pinkfloydx33

Reputation: 12799

SelectionChangeCommitted fires when the user manipulates via the UI.

SelectionChangeCommitted is raised only when the user changes the combo box selection. Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx

Use SelectedIndexChanged or SelectedValueChanged

Upvotes: 7

Related Questions