📜 ⬆️ ⬇️

Handling user selections in the ComboBox

If there is a task to react to the choice of a user from the ComboBox drop-down list, then the obvious solution is to subscribe to the SelectedIndexChanged event:

ComboBox myComboBox = new ComboBox ( ) ;
myComboBox. SelectedIndexChanged += new System . EventHandler ( this . MyComboBox_SelectedIndexChanged ) ;


But the fact is that the SelectedIndexChanged event occurs even if the user did not select anything in the ComboBox, and the index was changed by your own code (for example, you added several elements and selected the last one). The output will be a subscription to the SelectionChangeCommitted event:
myComboBox.SelectionChangeCommitted += new System.EventHandler(this.cbLookIn_SelectionChangeCommitted);

After that, you can freely manipulate the contents of the myComboBox.Items collection or the SelectedIndex property, without fear that the code that should be executed when the user selects a new item will be executed.

')

Source: https://habr.com/ru/post/87512/


All Articles