INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How do I loop through a CheckedList

How do I loop through a CheckedList

(OP)
How do I loop through a CheckedListBox and look at the name and if the name is in a string, check it.

I can figure out how to get the name:

foreach (var item in chkSelectStatementColumns.Items)
{
if (lbStatementNames.Text.IndexOf(item.ToString()) != -1)
{
MessageBox.Show(item.ToString());
}
}

But how do I check it?

I can check it using a for loop but how to I get the text?

I can't seem to figure out how to do both: get the text of the checkbox and then check or uncheck it.

Thanks,

Tom

RE: How do I loop through a CheckedList

(OP)
I thought I figured it out but it doesn't work. If you change the state of any of the checkboxes, you get an error:

CODE

ktr = 0;

            foreach (var item in chkSelectStatementColumns.Items)
            {
                if (txtSelectStatement.Text.IndexOf(item.ToString()) != -1)
                {
                    chkSelectStatementColumns.SetItemCheckState(ktr, CheckState.Checked);
                    //MessageBox.Show(item.ToString());
                }
                ktr++;
            } 

When I change any of the items to checked, it gives me an error:

List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.

I can understand if I had added or deleted an item why would changing the value do it.

How do I get this to work?

Thanks,

Tom

RE: How do I loop through a CheckedList

(OP)
I figured it out.

CODE

for (int i = 0; i < chkSelectStatementColumns.Items.Count; i++)
{
    if (txtSelectStatement.Text.IndexOf(chkSelectStatementColumns.Items[i].ToString()) != -1)
    {
        chkSelectStatementColumns.SetItemCheckState(i, CheckState.Checked);
    }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close