Back to Devexpress

BaseCheckedListBoxControl.CheckMember Property

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

latest9.6 KB
Original Source

BaseCheckedListBoxControl.CheckMember Property

Gets or sets the name of the data source field that provides check states for listbox items.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue("")]
[DXCategory("Data")]
public virtual string CheckMember { get; set; }
vb
<DefaultValue("")>
<DXCategory("Data")>
Public Overridable Property CheckMember As String

Property Value

TypeDefaultDescription
StringString.Empty

A string value specifying a field name in the data source.

|

Remarks

This property is in effect when a list box control is bound to a data source via the BaseListBoxControl.DataSource property.

Use the CheckMember property to specify the field whose values specify item check states (see CheckedListBoxControl.CheckMode). Typically, the field specified by the CheckMember property should be of the Boolean or Nullable<Boolean> type. If the field is of another type, you can convert it to the Boolean or Nullable<Boolean> type with the BaseCheckedListBoxControl.ConvertCheckValue event.

When an item’s check state is changed, a corresponding field value in the data source is changed as well.

To get the item check states, use the BaseCheckedListBoxControl.GetItemCheckState method.

Example

The following code demonstrates how you can bind the CheckedListBoxControl to a data source and obtain item captions and item check states from corresponding data source fields.

The BaseListBoxControl can be bound to a data source using the BaseListBoxControl.DataSource property. In this example, the data source is represented by the ArrayList object in which items are USState objects. The USState class represents a U.S. state. The class provides the LongName and ShortName properties specifying a state’s long and short name.

The long names of the states will be displayed as items in the list box, while the short names will specify list box item values. To accomplish this, the BaseListBoxControl.DisplayMember property is set to “LongName“ and the BaseListBoxControl.ValueMember property is set to “ShortName“.

The USState class also provides the IsSelected property. This property will specify the item check states. Set the BaseCheckedListBoxControl.CheckMember property to “IsSelected“.

When the application form is initialized, check boxes of list box items are set to the states specified by the underlaying data source. Toggling a check box affects the value of the IsSelected field in the data source.

The application form also contains a text box, displayed below the CheckedListBoxControl. If an end-user selects an item in the CheckedListBoxControl, this item’s value is displayed in the text box. The BaseListBoxControl.SelectedIndexChanged event is handled for this purpose.

csharp
using DevExpress.XtraEditors;

namespace CheckValueMember {
    public partial class Form1 : Form {
        CheckedListBoxControl listBox1;
        TextBox textBox1;
        public Form1() {
            InitializeComponent();

            ArrayList states1 = new ArrayList();
            states1.Add(new USState("Alabama", "AL", true));
            states1.Add(new USState("Washington", "WA", false));
            states1.Add(new USState("West Virginia", "WV", false));
            states1.Add(new USState("Wisconsin", "WI", null));
            states1.Add(new USState("Wyoming", "WY", null));

            listBox1 = new CheckedListBoxControl();
            listBox1.Location = new System.Drawing.Point(10, 10);
            listBox1.Size = new System.Drawing.Size(175, 137);

            listBox1.DataSource = states1;
            listBox1.DisplayMember = "LongName";
            listBox1.ValueMember = "ShortName";
            listBox1.CheckMember = "IsSelected";

            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;

            textBox1 = new TextBox();
            textBox1.Location = new System.Drawing.Point(10, 157);
            textBox1.Size = new System.Drawing.Size(175, 24);

            this.Controls.AddRange(new Control[] { this.listBox1, this.textBox1 });
        }

        void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
            this.textBox1.Text = this.listBox1.SelectedValue.ToString();
        }
    }
    public class USState {
        private string myShortName;
        private string myLongName;
        private bool? isSelected;

        public USState(string strLongName, string strShortName, bool? boolIsSelected) {
            this.myShortName = strShortName;
            this.myLongName = strLongName;
            this.isSelected = boolIsSelected;
        }

        public string ShortName {
            get {
                return myShortName;
            }
        }

        public string LongName {
            get {
                return myLongName;
            }
        }

        public bool? IsSelected {

            get {
                return isSelected;
            }
            set {
                isSelected = value;
            }
        }

    }
}
vb
Imports DevExpress.XtraEditors

Namespace CheckValueMember
    Public Partial Class Form1
        Inherits Form
        Private listBox1 As CheckedListBoxControl
        Private textBox1 As TextBox
        Public Sub New()
            InitializeComponent()

            Dim states1 As New ArrayList()
            states1.Add(New USState("Alabama", "AL", True))
            states1.Add(New USState("Washington", "WA", False))
            states1.Add(New USState("West Virginia", "WV", False))
            states1.Add(New USState("Wisconsin", "WI", Nothing))
            states1.Add(New USState("Wyoming", "WY", Nothing))

            listBox1 = New CheckedListBoxControl()
            listBox1.Location = New System.Drawing.Point(10, 10)
            listBox1.Size = New System.Drawing.Size(175, 137)

            listBox1.DataSource = states1
            listBox1.DisplayMember = "LongName"
            listBox1.ValueMember = "ShortName"
            listBox1.CheckMember = "IsSelected"

            AddHandler listBox1.SelectedIndexChanged, AddressOf listBox1_SelectedIndexChanged

            textBox1 = New TextBox()
            textBox1.Location = New System.Drawing.Point(10, 157)
            textBox1.Size = New System.Drawing.Size(175, 24)

            Me.Controls.AddRange(New Control() {Me.listBox1, Me.textBox1})
        End Sub

        Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            Me.textBox1.Text = Me.listBox1.SelectedValue.ToString()
        End Sub
    End Class
    Public Class USState
        Private myShortName As String
        Private myLongName As String
        Private myIsSelected As System.Nullable(Of Boolean)

        Public Sub New(strLongName As String, strShortName As String, boolIsSelected As System.Nullable(Of Boolean))
            Me.myShortName = strShortName
            Me.myLongName = strLongName
            Me.myIsSelected = boolIsSelected
        End Sub

        Public ReadOnly Property ShortName() As String
            Get
                Return myShortName
            End Get
        End Property

        Public ReadOnly Property LongName() As String
            Get
                Return myLongName
            End Get
        End Property

        Public Property IsSelected() As System.Nullable(Of Boolean)

            Get
                Return myIsSelected
            End Get
            Set
                myIsSelected = value
            End Set
        End Property

    End Class
End Namespace

See Also

DisplayMember

ValueMember

CheckMode

BaseCheckedListBoxControl Class

BaseCheckedListBoxControl Members

DevExpress.XtraEditors Namespace