Ken Ken - 2 months ago 6
Vb.net Question

How To Bind to CheckedListBox.SelectedItems.Count

I am trying to bind a label to a CheckedListBox.CheckedItems.Count
I have tried a couple approaches to this and receive the message:


Cannot bind to the property or column Count on the DataSource.
Parameter name: dataMember


My Code is as Follows:

Dim BgCountBinding As Binding = New Binding("Text", BgCheckedListBox.CheckedItems, "Count")

' I have also tried this:
' Dim BgCountBinding As Binding = New Binding("Text", BgCheckedListBox, "CheckedItems.Count")

BgCountBinding.DataSourceUpdateMode = DataSourceUpdateMode.Never
BgCountBinding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged
BgCountBinding.NullValue = "0"
BgCountBinding.FormattingEnabled = True
BgCountBinding.FormatString = "#: {0}"


lblBGCount.DataBindings.Add(BgCountBinding)


I know the code is VB but if you have a C# version - I can and will be happy to convert it.

Answer

Since the CheckListBox doesn't support multi-selection, probably you mean CheckItems.Count. You can not bind to CheckItems.Count. To be notified about changing in CheckedItem.Count you should handle ItemCheck event of the CheckedListBox:

C#

this.checkedListBox1.ItemCheck += (s, ea) =>
{
    this.BeginInvoke(new Action(() =>
    {
        this.label1.Text = this.checkedListBox1.CheckedItems.Count.ToString();
    }));
};
Comments