2

So, i have a checklistbox that contains string values like:

  • asdf-432-qwer-vcxz
  • rewq-123-qwer-vcxz
  • rety-323-qw65-vcyt
  • kjhf-232-ouyy-bjkl
  • ...

And i have an array(onlineVaults) that contains some of the same values that in checklistbox like:

  • rety-323-qw65-vcyt
  • asdf-432-qwer-vcxz

Now i want to check only those values in checklistbox that are in the array. Others should be unchecked.

for (int i = 0; i < checklistbox.Items.Count; i++)
{
    if (onlineVaults.Contains(checklistbox.Items[i]))
    {
        checklistbox.SetItemChecked(i, true);
    }
}

I have tried it to figure out like this, but it doesn't work. It only checks one value and nothing else. What i should do?

11
  • Can you provide a concrete example (have you tried this with the very values given in the question)? Which item remains checked in the end? Commented Aug 10, 2015 at 11:26
  • What is the default state of each checkbox? have you tried adding the else block to your if statement that sets the boxes to unchecked? Commented Aug 10, 2015 at 11:28
  • 1
    @niklang: Well, it doesn't when I try. Are there any event handlers bound to your control, or any non-standard settings? Commented Aug 10, 2015 at 11:49
  • 1
    Provide more code please. Where does this onlineVaults came from? Are you absolutely sure that there are values and not nulls there ? Are there multi-threading involved ? Commented Aug 10, 2015 at 12:04
  • 1
    @Rubidium37: The SelectionMode property merely controls how many items can be highlighted. At any time, an unlimited number of items can still be checked. Note that the docs even say: "For CheckedListBox objects, multiple selection is not supported. You can set the mode to one item or no items." Commented Aug 10, 2015 at 12:50

1 Answer 1

1

Try this

var checklistbox = new List<string>
{
     "asdf-432-qwer-vcxz",
     "rewq-123-qwer-vcxz",
     "rety-323-qw65-vcyt",
     "kjhf-232-ouyy-bjkl"
};
var onlineVaults = new List<string>
{
     "rety-323-qw65-vcyt",
     "asdf-432-qwer-vcxz"
};

for (int i = 0; i < checklistbox.Items.Count; i++)
{
    checklistbox.SetItemChecked(i, onlineVaults.Contains(checklistbox.Items[i]));            
}

EDIT:

To ignore case and trim values, try this

for (int i = 0; i < checklistbox.Items.Count; i++)
{
    checklistbox.SetItemChecked(i, IsValueExist(onlineVaults, checklistbox.Items[i]));            
}

private bool IsValueExist(List<string> list, string value)
{
    return list.Any(x => string.Compare(x.Trim(), value.Trim(), StringComparison.InvariantCultureIgnoreCase) == 0);        
}
Sign up to request clarification or add additional context in comments.

5 Comments

Didn't work, does the same thing that my if else code.
@M.NasserJavaid: Isn't it this method?
@niklang Can you see values using debug, is there need to trim values?
Yes they are ok, values are like this: {D1E8AD81-XXX-4D9C-VFDC-3B7AD146T5DE} @M.NasserJavaid
I mean to say, if there are some alphabets case difference or if any item has white space in left or right side then there will be matching issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.