ComboBox fires SelectedIndexChanged event even when selecting same combo item in the ComboBox Options

codeling Posts: 1085 Points: 4548
Posted: Friday, November 3, 2017 8:57:02 AM

Both SelectedIndexChanged and SelectedValueChanged events of a combobox get fired when you click the combobox and then select from the dropdown list the item that is already currently selected.  That is, even if the SelectedIndex or SelectedValue never changes, just the act of clicking on the currently selected item in the dropdown list causes the event to fire. 

You can use the following solution too gets around the problem of firing an event when the user selects the currently selected item.

private System.Windows.Forms.ComboBox _valueTypeComboBox;
...

this._valueTypeComboBox.SelectedIndex = -1;

this._valueTypeComboBox.SelectedIndexChanged += new System.EventHandler(this._valueTypeComboBox_SelectedIndexChanged);
         
...

 
private void _valueTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this._valueTypeComboBox.SelectedValue != null)
    {
        if (this._valueTypeComboBox.Tag != this._valueTypeComboBox.SelectedValue)
        {
            this._valueTypeComboBox.Tag = this._valueTypeComboBox.SelectedValue;

           
            //other code
        }
  
    }
}

 

codeling Posts: 1085 Points: 4548
Posted: Friday, November 3, 2017 8:59:08 AM

SelectionChangeCommitted behaves the same as SelectedIndexChanged, that is it get fired when you select the already selected item from the dropdown.

Users browsing this topic
Guest