When Calls to a CheckedListBox's SetItemChecked Method Don't Work

Album Cover: White Blood Cells

"Every breath that is in your lungs is a tiny little gift to me."
White Stripes / Dead Leaves and the Dirty Ground

Posted on January 20, 2010 5:30 PM in Programming
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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

Gary on May 27, 2014 at 8:46 AM:

Are you sorting? I just noticed i had to turn off sorting before I reloaded the checkbox list

clbFileList.Sorted = false;
clbFileList.Items.Clear();

Permalink

High PR Niche Relevant backlinks on September 05, 2016 at 2:50 AM:

I wish more writers of this sort of substance would take the time you did to explore and compose so well. I am exceptionally awed with your vision and knowledge.

Permalink

easy healthed on September 26, 2016 at 10:49 PM:

This immediate commitment regarding senior management as well as participation regarding employees in the development regarding workplace well-being policies are a vital prerequisite to your successful execution of policies that could address the exact needs on the working local community.

Permalink

durham healthy homes on September 26, 2016 at 10:50 PM:

Typically numerous only think about a family satisfying place when planning for a vacation; but it’s likewise imperative to afford quite the maximum amount of attention due to this very essential aspect when deciding to order a home whether they are the 1st buyers or perhaps not.

Permalink

sports men forken on September 26, 2016 at 10:50 PM:

Some people enjoy accomplishing sports along with the optimum time to think about something new and interesting or devote more time to enjoying a common sport will be during holiday break! Travel corporations attract in conjunction with exciting helps ensure: Bungee moving, rock camping, scuba deep-sea diving, sky deep-sea diving, perfect alpine winter sports slopes – that is certainly a thrilling time! And quite possibly a cause of future problems.

Permalink

netflix on October 10, 2016 at 1:23 AM:

Simply respecting your work and thinking about how you dealt with this online journal so well. It's remarkable to the point that I can't bear to not experience this significant data at whatever point I surf the web! minecraft free free netflix account

Permalink

kecibcbq on May 15, 2017 at 5:23 AM:

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.

Name:

Email Address:

Website:

Comments:

Check this box if you hate spam.