user1356310
user1356310

Reputation: 11

Why ItemCheck Event Of CheckList Box Always One Behind?

I am working on a winform in which I have taken a checklist box. I want to store the checked items of checklist box in array list.There are 5 items in my checklist box and I want to handle itemcheck event.

if I check one item itemcheck event fires but items count remains 0 again if I check 2 items in event handler items count becomes 1 if I repeat this process next time count becomes 2(but now I have checked 3 items).

Please help me on this code below is the code snippet that I am using :

 private void CLB_Course_ItemCheck(object sender, ItemCheckEventArgs e)
        {            
            List<string> items = new List<string>();            
            foreach (string ItemsChecked in CLB_Course.CheckedItems)
            {
                items.Add(ItemsChecked);
            }
        }

Upvotes: 1

Views: 262

Answers (2)

MarcoM
MarcoM

Reputation: 92

That occur because that event is raised when an item is about to have its checked state changed. The value is not updated until after the event occurs

Upvotes: 0

Patrick
Patrick

Reputation: 17973

As is explained in the question that Mitja linked to, CheckedListBox isn't that good.

If you want a list of checked items, you can consider using a ListView instead, it has a CheckBoxes property that you can set to true to get the same behaviour as you would get in a CheckedListBox, but with the added functionality of ItemChecked, since that event actually exist in the ListView control.

In your event listener, you can either get the checked item with the e.Item property (from ItemCheckedEventArgs), or get all checked items using the ListView's CheckedIndices, or CheckedItems properties.

Upvotes: 1

Related Questions