BaseListBoxControl.SelectedIndex Property
Gets or sets the index of the currently selected item.
Namespace: DevExpress.XtraEditors
Assembly: DevExpress.XtraEditors.v24.2.dll
Declaration
Property Value
Type | Description |
---|---|
Int32 | An integer value representing the zero-based index of the item in the list box control. -1 if no item is selected. |
Remarks
If single-selection mode is applied to the list box control (the BaseListBoxControl.SelectionMode property value is set to SelectionMode.One), this property holds the index of the currently selected item. Otherwise, if the control is in multi-select mode (the BaseListBoxControl.SelectionMode property value is set either to SelectionMode.MultiSimple or SelectionMode.MultiExtended), indexes of all currently selected items represent the collection which can be accessed via BaseListBoxControl.SelectedIndices property. In this instance, the SelectedIndex property contains the first element in BaseListBoxControl.SelectedIndices.
If you want to obtain the currently selected item, use the BaseListBoxControl.SelectedItem property. In addition, use the BaseListBoxControl.SelectedValue property to obtain the value of the currently selected item.
Changing the SelectedIndex property value raises the BaseListBoxControl.SelectedIndexChanged and BaseListBoxControl.SelectedValueChanged events.
Note: setting the SelectedIndex property to a value greater than the number of items immediately sets it to the last item’s index.
Example
The following sample code declares a DeleteItems method accepting two parameters:
- listBoxControl - a ListBoxControl;
- index - the index of an item after which all items are to be removed.
Use this method to delete all items from the ListBoxControl items collection that are next to the specified one. If the listBoxControl parameter points to a non-existent control or the index parameter points either to the non-existent control or the last item in the collection, method execution will not take place.
Note: if the control specified by the listBoxControl parameter is bound to a data source, method execution will not take place.
private void DeleteItems(ListBoxControl listBox, int index){
int count = listBox.ItemCount;
if (index < listBox.ItemCount - 1)
for (int i = index + 1; i < count; i ++)
listBox.Items.RemoveAt(index + 1);
}
// ...
// Deletes all items from the ListBoxControl collection after the selected one
DeleteItems(listBoxControl1, listBoxControl1.SelectedIndex);
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the SelectedIndex property.
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.