Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Respond to Selecting ListBoxControl's Items

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:

BaseListBoxControl - SelectedIndexChanged - example

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;
}