Amazing User
Amazing User

Reputation: 3563

Determination of the number of checked items of the CheckedListBox, WinForms

In my program for WinForms I have element CheckedListBox.

I have two problems with it:

  1. It is necessary to disable the ability to mark the new fields if already was marked 3;
  2. It should not completely disable an element, like I do. If you check 3 items and then uncheck one of them, then again, it should be possible to makr one more. And I completely disable CheckedListBox. I do not know how to do it right.

enter image description here

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Always print '1':
    MessageBox.Show(checkedListBox1.SelectedItems.Count.ToString());
    if (checkedListBox1.SelectedItems.Count == 3)
        checkedListBox1.Enabled = false;
}

Upvotes: 1

Views: 1196

Answers (3)

glenebob
glenebob

Reputation: 1973

Current answers are good and simple. Here's another approach that's a tad more involved. When a fourth item is checked, the least recently checked item is unchecked.

    LinkedList<int> checkedItemQueue = new LinkedList<int>();

    var clb = (CheckedListBox)sender;

    if (e.CurrentValue != CheckState.Checked && e.NewValue == CheckState.Checked)
    {
        checkedItemQueue.AddFirst(e.Index);

        while (checkedItemQueue.Count > 3)
        {
            clb.SetItemChecked(checkedItemQueue.Last.Value, false);
        }
    }
    else if (e.CurrentValue == CheckState.Checked && e.NewValue != CheckState.Checked)
    {
        var node = checkedItemQueue.Find(e.Index);

        if (node != null)
        {
            checkedItemQueue.Remove(node);
        }
    }

Upvotes: 1

Igor Popov
Igor Popov

Reputation: 2144

You should use CheckedItems (https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems(v=vs.110).aspx) instead of SelectedItems. The later are items that you selected (made coloured) and has no connection to checking.

Concerning your second problem a simple solution might be subscribing to CheckedListBox.ItemCheck event (https://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.itemcheck(v=vs.110).aspx). You can roll back user checking with some user friendly message here. If disabling is what you want to perform consider two simple ListBox components with "<" and ">" buttons between them. This case you can disable button when too many items are placed to selected list.

Upvotes: 0

Steve
Steve

Reputation: 216283

If I understand well your requirements you need to handle the ItemCheck event with this code

private void checkListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
    CheckedListBox chk = sender as CheckedListBox;
    if(e.NewValue == CheckState.Checked && chk.CheckedItems.Count > 2)
        e.NewValue = CheckState.Unchecked;
}

In this way the CheckListBox is never disabled, you let your user check and uncheck at will. But when you receive the ItemCheck event you verify how many elements are checked and, if you have reached your limit, simply set the NewValue property of the ItemCheckEventArgs to an Unchecked state.

Upvotes: 3

Related Questions