Back to Devexpress

BaseListBoxControl.SelectedIndexChanged Event

windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-8e09b85f.md

latest5.4 KB
Original Source

BaseListBoxControl.SelectedIndexChanged Event

Allows you to respond to item selection.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Property Changed")]
public event EventHandler SelectedIndexChanged
vb
<DXCategory("Property Changed")>
Public Event SelectedIndexChanged As EventHandler

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

In this example, a ListBoxControl displays a list of system colors. The following code handles the BaseListBoxControl.SelectedIndexChanged event to change the form’s background color when a user selects a color in the listbox:

csharp
private void Form1_Load(object sender, EventArgs e) {
    Color[] colorArray = {
                   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
                };
    listBoxControl1.DataSource = colorArray;
    listBoxControl1.DisplayMember = "Name";
    listBoxControl1.SelectedIndexChanged += ListBoxControl1_SelectedIndexChanged;
}

private void ListBoxControl1_SelectedIndexChanged(object sender, EventArgs e) {
    if (listBoxControl1.SelectedValue != null)
        this.BackColor = (Color)listBoxControl1.SelectedValue;
    else
        this.BackColor = Color.Black;
}
vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim colorArray() As Color = {
        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}
    ListBoxControl1.DataSource = colorArray
    ListBoxControl1.DisplayMember = "Name"
    AddHandler ListBoxControl1.SelectedIndexChanged, AddressOf ListBoxControl1_SelectedIndexChanged
End Sub

Private Sub ListBoxControl1_SelectedIndexChanged(sender As Object, e As EventArgs)
    If ListBoxControl1.SelectedValue IsNot Nothing Then
        Me.BackColor = CType(ListBoxControl1.SelectedValue, Color)
    Else
        Me.BackColor = Color.Black
    End If
End Sub

See Also

SelectedIndex

SelectedValue

SelectedValueChanged

BaseListBoxControl Class

BaseListBoxControl Members

DevExpress.XtraEditors Namespace