Skip to main content
A newer version of this page is available. .

BaseListBoxControl.SelectedIndexChanged Event

Allows you to respond to item selection.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v18.2.dll

Declaration

[DXCategory("Property Changed")]
public event EventHandler SelectedIndexChanged

Event Data

The SelectedIndexChanged event's data class is EventArgs.

Remarks

Handle the SelectedIndexChanged event to perform actions each time the selection has been changed. If the control functions in single selection mode, this event fires after the BaseListBoxControl.SelectedIndex property value has been changed. Usually this happens in the following cases:

  • focus moves from one item to another;
  • the item that was selected has been removed.

The list box control can, however, function in multi selection mode, allowing several items to be selected. In this case, the SelectedIndexChanged event fires each time the selection has been changed. The indexes of all items currently selected comprise the collection which can be accessed via control’s BaseListBoxControl.SelectedIndices property. The BaseListBoxControl.SelectedIndex property contains the first element in the collection in this case.

Example

The ListBoxControl displays system color names. The following sample code handles the BaseListBoxControl.SelectedIndexChanged event to change the form’s background color to the color whose name is currently selected. The formBkColors hashtable is created for this purpose. Its key is a string representing the system color name. The value, in turn, represents the proper color object. Hence, when a desired color name is selected, the proper color is assigned to the form’s BackColor property.

The image below demonstrates the ListBoxControl‘s look & feel after sample code execution.

BaseListBoxControl-SelectedIndexChanged

// Creates and initializes a new Hashtable.
Hashtable formBkColors = new Hashtable();
// Creates an array containing system colors
Color[] sysColors = {
                           SystemColors.ActiveCaption,
                           SystemColors.ActiveCaptionText,
                           SystemColors.AppWorkspace,
                           SystemColors.Control,
                           SystemColors.ControlDark,
                           SystemColors.ControlLight,
                           SystemColors.ControlText,
                           SystemColors.Desktop,
                           SystemColors.Highlight,
                           SystemColors.InactiveBorder,
                           SystemColors.InactiveCaption,
                           SystemColors.Info,
                           SystemColors.InfoText,
                           SystemColors.Menu,
                           SystemColors.MenuText,
                           SystemColors.ScrollBar,
                           SystemColors.Window,
                           SystemColors.WindowFrame
                        };
// ...
for(int i = 0; i < sysColors.Length; i++){
   // Fills the hashtable
   formBkColors[sysColors[i].Name] = sysColors[i];
   // Populates the ListBoxControl items collection
   listBoxControl1.Items.Add(sysColors[i].Name);
}
private void listBoxControl1_SelectedIndexChanged(object sender, System.EventArgs e) {
   this.BackColor = (Color)formBkColors[(sender as ListBoxControl).Text];
}

The following code snippets (auto-collected from DevExpress Examples) contain references 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.

See Also