SoFunction
Updated on 2025-03-07

C# SelectedIndexChanged event detailed explanation

SelectionChangeCommitted Event

This event is not triggered when setting datasource, displaymember and value, and manually changing the combobox index and value, but is triggered when selecting different combobox values ​​on the interface.

SelectedIndexChanged event occurs when the SelectedIndex property changes and triggers when the index changes.

SelectedValueChanged event   Occurs when the SelectedValue property changes and triggers when the value changes.

the difference:

  1. SelectedIndexChanged and SelectedValueChanged can trigger events by changing properties programmatically, but the SelectionChangeCommitted event must be triggered by the user's operation of the selected option.
  2. Both SelectedIndexChanged and SelectedValueChanged are called at initialization (set source), and SelectionChangeCommitted does not have this problem.
  3. SelectionChangeCommitted also has a small problem. When you open the drop-down menu and use the up and down cursor keys of the keyboard to select an entry (without clicking with the mouse), click on other controls with the mouse to shift the focus. At this time, the Text property of comboBox has changed, and the SelectedIndex property has also changed, but such an operation will not trigger the SelectionChangeCommitted event. Conclusion: SelectionChangeCommitted must be selected by the mouse to trigger (personal feeling).

About the triggering of events

Set the number of times DataSource, DisplayMember, and ValueMember triggers the SelectedIndexChanged event and the SelectedValueChanged event in different orders

Order 1:

this. = "userName";  (not triggered)

this. = "userAge";    (Triggers SelectedValueChanged only)

this. = dt; (all triggered)

SelectedValueChanged triggers twice (always triggered first) SelectedIndexChanged trigger, triggers once (the two events are not triggered when setting the DisplayMember property)

Sequence 2:

this. = "userAge";

this. = "userName";

this. = dt;

Same as above

Sequence three:

this. = dt; (all triggered)

this. = "userName";(both triggered)

this. = "userAge"; (only triggers the SelectedValueChanged event)

SelectedValueChanged triggered three times (always triggered first) SelectedIndexChanged triggered twice (the two events are not triggered when setting the DisplayMember property)

Sequence 4:

this. = dt; (all triggered)

this. = "userAge";(both triggered) where SelectedValueChanged triggers twice

this. = "userName";(both triggered)

SelectedValueChanged triggers four times (always triggered first) SelectedIndexChanged trigger, three times (the two events are not triggered when setting the DisplayMember property)

Note: When the DataSource property is set, the SelectedValue property value will default to the first line (so the SelectedIndexChanged event and the SelectedValueChanged event will be triggered). Therefore, if you do not want ComboBox to automatically select the first line, you need to set SelectedValue to "" after setting the DataSource.

This is the end of this article about the detailed explanation of the C# SelectedIndexChanged event. For more related content of the C# SelectedIndexChanged event, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!