Back to Devexpress

BaseCheckedListBoxControl.ItemCheck Event

windowsforms-devexpress-dot-xtraeditors-dot-basecheckedlistboxcontrol-f3c36188.md

latest12.8 KB
Original Source

BaseCheckedListBoxControl.ItemCheck Event

Fires after an item’s check state changes.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Behavior")]
public event ItemCheckEventHandler ItemCheck
vb
<DXCategory("Behavior")>
Public Event ItemCheck As ItemCheckEventHandler

Event Data

The ItemCheck event's data class is ItemCheckEventArgs. The following properties provide information specific to this event:

PropertyDescription
IndexGets the index of the item whose state was changed.
StateGets the state of the item.

Remarks

The ItemCheck event fires when an item’s CheckState property is changed by the user or in code. To get the new check state, read the State argument. The Index event argument returns the processed item’s index. If items in the list box are filtered (see SearchControl), this argument returns the item’s index in the filtered list.

The ItemChecking event fires before the check state changes. The event allows you to cancel the action.

Example

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

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

Unbound Mode

csharp
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");
        }
    }
}
vb
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

The following code snippets (auto-collected from DevExpress Examples) contain references to the ItemCheck event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

xaf-how-to-display-a-collection-property-as-a-checked-list-box/CS/DXExample.Module.Win/WinCheckedListBoxPropertyEditor.cs#L23

csharp
if (PropertyValue is XPBaseCollection) {
    Control.ItemCheck -= new DevExpress.XtraEditors.Controls.ItemCheckEventHandler(control_ItemCheck);
    checkedItems = (XPBaseCollection)PropertyValue;

winforms-dashboard-custom-properties/CS/WinForms-Dashboard-Custom-Properties/Modules/GridFixedColumnModule.cs#L86

csharp
}
checkedCombo.ItemCheck += CheckedCombo_ItemCheck;
checkedCombo.Dock = DockStyle.Fill;

xaf-how-to-display-a-collection-property-as-a-checked-list-box/VB/DXExample.Module.Win/WinCheckedListBoxPropertyEditor.vb#L27

vb
If TypeOf PropertyValue Is XPBaseCollection Then
    RemoveHandler Control.ItemCheck, AddressOf control_ItemCheck
    checkedItems = CType(PropertyValue, XPBaseCollection)

winforms-dashboard-custom-properties/VB/WinForms-Dashboard-Custom-Properties/Modules/GridFixedColumnModule.vb#L89

vb
Next col
AddHandler checkedCombo.ItemCheck, AddressOf CheckedCombo_ItemCheck
checkedCombo.Dock = DockStyle.Fill

See Also

ItemChecking

SetItemChecked(Int32, Boolean)

SetItemCheckState(Int32, CheckState)

CheckMode

BaseCheckedListBoxControl Class

BaseCheckedListBoxControl Members

DevExpress.XtraEditors Namespace