windowsforms-9470-controls-and-libraries-editors-and-simple-controls-examples-how-to-respond-to-checking-items-in-checkedlistboxcontrol.md
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”).
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; }
}
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls
Imports System.ComponentModel
Namespace DXApplication
Partial Public Class Form1
Inherits XtraForm
Private items As BindingList(Of DataItem)
Public Sub New()
InitializeComponent()
items = New BindingList(Of DataItem)() From {
New DataItem() With {
.Name = "Item 0",
.Value = 0
},
New DataItem() With {
.Name = "Item 1",
.Value = 1,
.Checked = True
},
New DataItem() With {
.Name = "Item 2",
.Value = 2
}
}
checkedListBoxControl1.DataSource = items
checkedListBoxControl1.DisplayMember = "Name"
checkedListBoxControl1.ValueMember = "Value"
checkedListBoxControl1.CheckMember = "Checked"
AddHandler checkedListBoxControl1.ItemChecking, AddressOf CheckedListBoxControl1_ItemChecking
AddHandler checkedListBoxControl1.ItemCheck, AddressOf CheckedListBoxControl1_ItemCheck
End Sub
Private Sub CheckedListBoxControl1_ItemChecking(ByVal sender As Object, ByVal e As ItemCheckingEventArgs)
Dim list As CheckedListBoxControl = DirectCast(sender, CheckedListBoxControl)
Dim item As DataItem = CType(list.GetItem(e.Index), DataItem)
e.Cancel = item.Name = "Item 0"
End Sub
'Display a message box after an item's check state changes.
Private Sub CheckedListBoxControl1_ItemCheck(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs)
Dim checkedListBox As CheckedListBoxControl = DirectCast(sender, CheckedListBoxControl)
Dim item As DataItem = CType(checkedListBox.GetItem(e.Index), DataItem)
XtraMessageBox.Show($"The check state of '{item.Name}' was changed. New state: {e.State}", "Information")
End Sub
End Class
Public Class DataItem
Public Property Name() As String
Public Property Value() As Integer
Public Property Checked() As Boolean
End Class
End Namespace
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");
}
}
}
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls
Imports System.Windows.Forms
Namespace DXApplication
Partial Public Class Form1
Inherits XtraForm
Public Sub New()
InitializeComponent()
checkedListBoxControl1.Items.AddRange(New CheckedListBoxItem(){
New CheckedListBoxItem(0, "Item 0"),
New CheckedListBoxItem(1, "Item 1", CheckState.Checked),
New CheckedListBoxItem(2, "Item 2")
})
AddHandler checkedListBoxControl1.ItemChecking, AddressOf CheckedListBoxControl1_ItemChecking
AddHandler checkedListBoxControl1.ItemCheck, AddressOf CheckedListBoxControl1_ItemCheck
End Sub
Private Sub CheckedListBoxControl1_ItemChecking(ByVal sender As Object, ByVal e As ItemCheckingEventArgs)
Dim list As CheckedListBoxControl = DirectCast(sender, CheckedListBoxControl)
Dim item As CheckedListBoxItem = CType(list.GetItem(e.Index), CheckedListBoxItem)
e.Cancel = item.Description = "Item 0"
End Sub
'Display a message box after an item's check state changes.
Private Sub CheckedListBoxControl1_ItemCheck(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs)
Dim checkedListBox As CheckedListBoxControl = DirectCast(sender, CheckedListBoxControl)
Dim item As CheckedListBoxItem = CType(checkedListBox.GetItem(e.Index), CheckedListBoxItem)
XtraMessageBox.Show($"The check state of '{item.Description}' was changed. New state: {e.State}", "Information")
End Sub
End Class
End Namespace