in

Bunker Hollow

Matt Williamson's home on the web, welcome.

Sponsored Link

Matt Williamson's Blog

Personal discoveries of an IT professional.

The CheckedListBox Control: A Microsoft Blunder

I'm developing a new .NET 3.5 winform application and in a preferences dialog there is a group of options that will be selected with checkboxes.  I opened up the toolbox and started the cursor towards a GroupBox control when I noticed the newer CheckedListBox and thought, "Man, it sure is nice having the latest development tools, I bet this is the better way to do it."

So I create the control and fill it with the dozen or so checked preferences I need.  Then, imagine this crazy scenario, I want to iterate through the checkboxes!  I retrieve the preferences from the database and now I want to check each box that should be checked.  After trying, failing, and then researching the issue it turns out:

  • The CheckedListBox Items property is not a collection of CheckBox controls, and can't be cast as one either.
  • The CheckedListBox contains a CheckedItemsCollection, but no UncheckedItemsCollection.

After spending the time to make these discoveries I've found the numerous ways to solve the problem, so I understand it's possible, but I've got to say it's very poorly implemented.

UPDATE:  I assumed it should function as a group of Checkbox objects.  It doesn't.  My bad.

UPDATE:  I posted some sample code.  After reading Allan's comment below I revisited this post and I still agree that this control is unnecessarily difficult to pickup and use.  I came up with the following CheckedListBox sample solution, which you can download here.

private void btnCheckAll_Click( object sender, EventArgs e )
{
    for( int i = 0; i < checkedListBox1.Items.Count; i++ )
        checkedListBox1.SetItemChecked( i, true );
}

private void btnCheckRandom_Click( object sender, EventArgs e )
{
    Random r = new Random();
    for( int i = 0; i < checkedListBox1.Items.Count; i++ )
    {
        bool check = false;  // we won't check the item if the random number is odd

        if( r.Next() % 2 == 0 )
            check = true;  // the random number is even so we'll check it

        checkedListBox1.SetItemChecked( i, check );
    }
}

private void btnCheckInverse_Click( object sender, EventArgs e )
{
    for( int i = 0; i < checkedListBox1.Items.Count; i++ )
    {
        bool check = checkedListBox1.GetItemChecked( i );
        checkedListBox1.SetItemChecked( i, !check );  // set item to opposite of it's current state
    }
}

private void btnCheckNone_Click( object sender, EventArgs e )
{
    for( int i = 0; i < checkedListBox1.Items.Count; i++ )
        checkedListBox1.SetItemChecked( i, false );
}

Comments

 

Allan Clarke said:

Ack. I'm in the same spot, just about to figure out how to get at the CheckBox control for each item. I wish you had posted some kind of resolution.

March 3, 2010 9:28 AM
 

Matt Williamson said:

Thanks for the comment Allan.  I posted some sample code, hope it helps.

March 3, 2010 11:22 AM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems