web analytics
ComboBox fires SelectedIndexChanged event even when selecting same combo item in the ComboBox Options
codeling
Posted: Friday, November 3, 2017 8:57:02 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 916
Points: 3849

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

Sponsor
Posted: Friday, November 3, 2017 8:57:02 AM
 
codeling
Posted: Friday, November 3, 2017 8:59:08 AM

Rank:Advanced Member
Groups: Member
Joined: 12/11/2015
Posts: 916
Points: 3849

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

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2017 Digcode.com. All rights reserved.