I encountered what appears to be a bug (or a really lame limitation) in .NET today while working with a CheckedListBox control. In the application I'm working on, the contents of a CheckedListBox are manipulated based on the user's interaction with another form control. Based on the user's actions in that other control, Items are either added to (via Add()) or removed from (via Remove()) the CheckedListBox. This is pretty straightforward.
For whatever reason, there exists a scenario in which Items added to a previously empty CheckedListBox and then subsequently checked using the SetItemChecked() method show up in the CheckedListBox as unchecked. Here is some sample code demonstrating my usage of the various methods:
// add the item
myCheckedListBox.Items.Add(itemToAdd);
// find the newly added item and change its CheckState to checked
myCheckedListBox.SetItemChecked(myCheckedListBox.Items.IndexOf(itemToAdd), true);
In my particular application, the above calls are made within a loop, where itemToAdd represents a different item upon each iteration. What I see in my application is that, if myCheckedListBox is empty (i.e. contains no items) when the loop starts executing, when the loop is finished, the added items will be visible, but none of them will be checked. However, if myCheckedListBox already contains items when the loop starts executing, the newly added items are both visible and checked, as desired (and as advertised by the documentation of the code in question).
I researched this problem quite a bit and, unfortunately, found nothing that explained this particular scenario. The closest explanation I could find was a six-year-old discussion of the implications of loading items into a non-visible, data-bound CheckedListBox. Though the issue does seem very similar to the one I'm experiencing, my CheckedListBox is neither data-bound (I add and remove items manually) nor is it non-visible. Nevertheless, I experience the same symptom.
Whether the problem is really a limitation of the .NET framework or simply a pesky bug, I needed to find a way to work around it. Luckily, I was able to find a relatively straightforward workaround. To address the problem, I added some checks in my code for the special case where items are being added in the loop when the CheckedListBox was previously empty. If this scenario is encountered, after the loop finishes executing, I've added some code that iterates through the newly added items in another loop and sets their state to checked again:
// iterate through all items
for (int i = 0; i < myCheckedListBox.Items.Count; i++)
{
// change the state to checked
myCheckedListBox.SetItemChecked(i, true);
}
It seems redundant to do so, of course, but something about this happening outside of the initial loop execution leads to the desired results. And in the end, it's all about achieving the desired results, right?
Comments
No one has added any comments.
Post Comments
If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.