Skip to main content

How to: Respond to Checking Items in CheckedListBoxControl

  • 4 minutes to read

The following example displays a message box after an item’s check state changes. The example prevents users from toggling the check state of the item with the specified name/description (“Item 0”).

Bound Mode

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.ComponentModel;

namespace DXApplication {
    public partial class Form1 : XtraForm {
        BindingList<DataItem> items;
        public Form1() {
            InitializeComponent();
            items = new BindingList<DataItem>() {
                new DataItem(){ Name = "Item 0", Value = 0 },
                new DataItem(){ Name = "Item 1", Value = 1, Checked = true },
                new DataItem(){ Name = "Item 2", Value = 2 }
            };
            checkedListBoxControl1.DataSource = items;
            checkedListBoxControl1.DisplayMember = "Name";
            checkedListBoxControl1.ValueMember = "Value";
            checkedListBoxControl1.CheckMember = "Checked";
            checkedListBoxControl1.ItemChecking += CheckedListBoxControl1_ItemChecking;
            checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck;
        }
        void CheckedListBoxControl1_ItemChecking(object sender, ItemCheckingEventArgs e) {
            CheckedListBoxControl list = (CheckedListBoxControl)sender;
            DataItem item = (DataItem)list.GetItem(e.Index);
            e.Cancel = item.Name == "Item 0";
        }
        //Display a message box after an item's check state changes.
        void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) {
            CheckedListBoxControl checkedListBox = (CheckedListBoxControl)sender;
            DataItem item = (DataItem)checkedListBox.GetItem(e.Index);
            XtraMessageBox.Show($"The check state of '{item.Name}' was changed. New state: {e.State}", "Information");
        }
    }
    public class DataItem {
        public string Name { get; set; }
        public int Value { get; set; }
        public bool Checked { get; set; }
    }
}

Unbound Mode

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.Windows.Forms;

namespace DXApplication {
    public partial class Form1 : XtraForm {
        public Form1() {
            InitializeComponent();
            checkedListBoxControl1.Items.AddRange(new CheckedListBoxItem[]{
                new CheckedListBoxItem(0, "Item 0"),
                new CheckedListBoxItem(1, "Item 1", CheckState.Checked),
                new CheckedListBoxItem(2, "Item 2"),
            });
            checkedListBoxControl1.ItemChecking += CheckedListBoxControl1_ItemChecking;
            checkedListBoxControl1.ItemCheck += CheckedListBoxControl1_ItemCheck;
        }
        void CheckedListBoxControl1_ItemChecking(object sender, ItemCheckingEventArgs e) {
            CheckedListBoxControl list = (CheckedListBoxControl)sender;
            CheckedListBoxItem item = (CheckedListBoxItem)list.GetItem(e.Index);
            e.Cancel = item.Description == "Item 0";
        }
        //Display a message box after an item's check state changes.
        void CheckedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e) {
            CheckedListBoxControl checkedListBox = (CheckedListBoxControl)sender;
            CheckedListBoxItem item = (CheckedListBoxItem)checkedListBox.GetItem(e.Index);
            XtraMessageBox.Show($"The check state of '{item.Description}' was changed. New state: {e.State}", "Information");
        }
    }
}