Back to Devexpress

How to: Disable Specific Items in CheckedListBoxControl via Event

windowsforms-9472-controls-and-libraries-editors-and-simple-controls-examples-how-to-disable-specific-items-in-checkedlistboxcontrol-via-event.md

latest2.4 KB
Original Source

How to: Disable Specific Items in CheckedListBoxControl via Event

  • Oct 25, 2019
  • 2 minutes to read

The following example shows how to disable specific items in a checked list box control via the BaseCheckedListBoxControl.GetItemEnabled event.

Assume that a CheckedListBoxControl is bound to a BindingSource object containinig information on products. The data source contains a ProductName and Discontinued fields. The values of the ProductName field are displayed in the control. The values of the Discontinued field are used to decide whether a corresponding check item should be disabled.

The result is displayed below:

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

// Assign a data source to the checked list box.
checkedListBoxControl1.DataSource = productsBindingSource;
checkedListBoxControl1.DisplayMember = "ProductName";

// Disable the items that have the Discontinued field set to true
private void checkedListBoxControl1_GetItemEnabled(object sender, GetItemEnabledEventArgs e) {
    CheckedListBoxControl control = sender as CheckedListBoxControl;
    bool isDiscontinued = (bool)((control.DataSource as BindingSource)[e.Index] as 
        DataRowView)["Discontinued"];
    if (isDiscontinued)
        e.Enabled = false;
}
vb
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls

' Assign a data source to the checked list box.
Private checkedListBoxControl1.DataSource = productsBindingSource
Private checkedListBoxControl1.DisplayMember = "ProductName"

' Disable the items that have the Discontinued field set to true
Private Sub CheckedListBoxControl1_GetItemEnabled(ByVal sender As System.Object, _
    ByVal e As GetItemEnabledEventArgs) _
    Handles CheckedListBoxControl1.GetItemEnabled
    Dim control As CheckedListBoxControl = TryCast(sender, CheckedListBoxControl)
    Dim isDiscontinued As Boolean = CBool((TryCast((TryCast(control.DataSource, _
        BindingSource))(e.Index), DataRowView))("Discontinued"))
    If isDiscontinued Then
        e.Enabled = False
    End If
End Sub