RepositoryItemComboBox.SelectedIndexChanged Event
Occurs on changing the index of the selected value in the combo box editor.
Namespace: DevExpress.XtraEditors.Repository
Assembly: DevExpress.XtraEditors.v24.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
Event Data
The SelectedIndexChanged event's data class is EventArgs.
Remarks
The SelectedIndexChanged event occurs on changing the value of the ComboBoxEdit.SelectedIndex property. This property denotes the index of the selected value, which is provided by the ComboBoxEdit.SelectedItem property and displayed in the control’s edit box.
The index of the selected value is changed when:
- another item is selected up from the dropdown list. In this instance, ComboBoxEdit.SelectedIndex is set to the index of the selected item.
- when the currently selected value represents an item from the RepositoryItemComboBox.Items collection and the new selected value does not belong to the collection. In this instance, ComboBoxEdit.SelectedIndex is set to -1.
You can handle the SelectedIndexChanged event in order to get notifications that the user has selected another item from the RepositoryItemComboBox.Items collection.
If the currently selected value does not exist in the item collection (i.e. ComboBoxEdit.SelectedIndex is -1) and the newly selected value does not also belong to the collection, the SelectedIndexChanged event is not fired since the value of the ComboBoxEdit.SelectedIndex property remains -1. However, in this case, the RepositoryItem.EditValueChanging and RepositoryItem.EditValueChanged events still occur.
Assume you have a combo box editor displaying two items: ‘Frankfurt’ and ‘London’ and the end-user is able to change the text in the edit region of the combo box (the RepositoryItemButtonEdit.TextEditStyle property is set to true). The currently selected item is ‘Frankfurt’ and ComboBoxEdit.SelectedIndex is 0.
If the user presses the ‘A’ key, the combo box looks through items and tries to locate the item starting with ‘A’ (the RepositoryItemComboBox.AutoComplete property must be set to true). Since this item cannot be found, the selected value is set to ‘A’, the index of the selected value is set to -1 and the SelectedIndexChanged event is fired. THe RepositoryItem.EditValueChanged event is also fired as a result of changing the edit value which matches the selected value.
Pressing the ‘m’ key changes the selected value to ‘Am’. The value of ComboBoxEdit.SelectedIndex does not change and the SelectedIndexChanged event is not fired. However, the RepositoryItem.EditValueChanged event still occurs.
The editor’s ComboBoxEdit.SelectedIndexChanged event is equivalent to the current event.
Example
Consider a combo box editor displaying different font styles (regular, bold, italic, underline, strikeout). When you select an item from the dropdown list, a corresponding font style is applied to the combo box editor.
Combo box items in this example represent strings identifying names of specific font styles. In the ComboBoxEdit.SelectedIndexChanged event handler, we determine the currently selected item and convert it to a corresponding value of type FontStyle. Then the new font is applied to the editor.
The following image shows a combo box editor when the Underline item is selected.
comboBoxEdit1.EditValue = "Regular";
comboBoxEdit1.Properties.Items.AddRange(new object[]
{"Bold", "Italic", "Regular", "StrikeOut", "Underline"});
comboBoxEdit1.SelectedIndexChanged += new System.EventHandler(
this.comboBoxEdit1_SelectedIndexChanged);
//...
private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) {
EnumConverter ec = TypeDescriptor.GetConverter(typeof(FontStyle)) as EnumConverter;
var cBox = sender as ComboBoxEdit;
if (cBox.SelectedIndex != -1) {
FontStyle selectedFontStyle = (FontStyle)ec.ConvertFrom(
cBox.Properties.Items[cBox.SelectedIndex]);
cBox.Properties.Appearance.FontStyleDelta = selectedFontStyle;
}
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SelectedIndexChanged event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.