找到ValueMember时应检查CheckedListBox [英] CheckedListBox should be checked when ValueMember is found
问题描述
在我的Winforms应用程序中,我使用CheckedListBox.当我更新此清单框时,我想要的是在更新之前,应检查先前选择的项目,这样我才能知道自己所检查的内容.
现在的问题是我将估值成员存储在数据库中.我获取此列表并尝试对其进行检查,但它要求输入索引号.
FindExactString("Text"),但我的valuemember是1001
值是1001
文字为测试"
我不知道如何使它工作.请帮帮我.
谢谢
Seema
这是一些代码:
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:
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);
}
推荐答案
不是将字符串添加到列表框中,而是添加具有ToString()
覆盖的对象,该对象会将所需内容放入列表框中.这样,当您获得选定的索引时,就可以访问对象的属性(当然具有适当的强制转换).
Instead of adding strings to your listbox, add objects with aToString()
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).
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)
{
...
}
这篇关于找到ValueMember时应检查CheckedListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!