web analytics

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

Options

codeling 1595 - 6639
@2017-11-03 08:57:02

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

@2017-11-03 08:59:08

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

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com