Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my Winforms application I use CheckedListBox. When I update this checklist box what I want is that before updating, the previous selected item should be checked so that I can know what I checked.

Now the issue is that I store the valumember in my database. I fetch this list and try to check it, but it asks for index number.

FindExactString("Text"), but I have valuemember that is 1001

"value is 1001
Text is Test"

I don't know how to get it working. Please help me.

Thank you
Seema

Here's some code:


C#
for(int i=0; i<teamlist.Length-1;i++)
{
    for (int k = 0; k < chklistbox.Items.Count; k++)
    {
            chklistbox.SelectedIndex = k;
//I comparing selectedIndex's ValueMemer is same that I fetch from database
            if(chklistbox.SelectedValue.ToString()== teamlist[i].ToString())
            {


                chklistbox.SetItemChecked(k, true);
            }
    }

    //chklistbox.SetItemChecked, true);
}
Posted
Updated 20-Jan-11 4:46am
v2
Comments
Henry Minute 20-Jan-11 10:00am    
Your question is not clear.
Perhaps it would help if you edit it to include a small code snippet (don't forget to surround it with <pre>your code here</pre> tags, if you do).

1 solution

Instead of adding strings to your listbox, add objects with a ToString() override that puts what you want in the listbox. That way, when you get the selected index, you can access the properties of the object (with the proper casting of course).

C#
public class MyItem
{
    public string Text { get; set; }
    public int Value { get; set; }

    public override ToString()
    {
        return this.Text;
    }
}

if (((MyItem)(listbox.SelectedItem)).Value == 0)
{
   ...
}
 
Share this answer
 
v3
Comments
Seema Gosain 20-Jan-11 11:22am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900