I have a checked list box with an unknown number of items some of which may have been checked. I need to know how to determine which of the items have been checked. For purposes of discussion, assume that my checked list box is called myCheckedListBox.

In theory, I need to scroll through the list as in

for (int x = 0; int < myCheckedListBox.Items.Count; x++)
{
    // what goes here to check each index/item = x to see if it's been checked?
}

Thanks..

Recommended Answers

All 5 Replies

CheckedIndices will return a collection of indexes that are checked.
CheckedItems will return a collection of items that are checked.
GetItemChecked() returns a boolean telling you if the specific index is checked.
GetItemCheckState() returns a CheckState for the specific index.

If you only care about the checked ones, the first two properties are the best to use.

Use CheckedItems property.

for (int i = 0; i < myCheckedListBox.CheckedItems.Count  ; i++)
{
  MessageBox.Show(myCheckedListBox.CheckedItems[i].ToString());
}

re: MessageBox.Show(myCheckedListBox.CheckedItems.ToString());

I'm not at all interested in displaying anything in a message box, or anywhere else. The functionality I need has to go item by item through the checked list and if any item is checked, then some other action has to be taken. I'm thinking of some syntax like: if(myCheckedListBox.Index[x].Checked == true)

I know this is not the correct C# syntax - but if you read my intention correctly, you'll understand what I'm trying to do, correctly! :)

foreach (whatEverMyItemTypeIs item in myCheckedListBox.CheckedItems) {
    // perform whatever action you need on item
}

Thanks for your help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.