Back to Devexpress

How to: Respond to Selecting ListBoxControl's Items

windowsforms-9459-controls-and-libraries-editors-and-simple-controls-examples-how-to-respond-to-selecting-listboxcontrols-items.md

latest3.1 KB
Original Source

How to: Respond to Selecting ListBoxControl's Items

  • Oct 25, 2019

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