的CheckedListBox - 搜索由文本项目 [英] CheckedListBox - Search for an item by text
问题描述
我有一个的CheckedListBox
绑定到数据表
。现在我需要以编程方式检查一些项目,但我发现 SetItemChecked(...)
方法只接受项目索引。
I have a CheckedListBox
bound to a DataTable
. Now I need to check some items programmatically, but I find that the SetItemChecked(...)
method only accepts the item index.
有没有一种实际的方式来获得由文/标签的项目,不知道项指数?
Is there a practical way to get an item by text/label, without knowing the item index?
(注:我已经得到了有限的经验的WinForms ...)
(NOTE: I've got limited experience with WinForms...)
推荐答案
您可以实现自己的 SetItemChecked(字符串项);
private void SetItemChecked(string item)
{
int index = GetItemIndex(item);
if (index < 0) return;
myCheckedListBox.SetItemChecked(index, true);
}
private int GetItemIndex(string item)
{
int index = 0;
foreach (object o in myCheckedListBox.Items)
{
if (item == o.ToString())
{
return index;
}
index++;
}
return -1;
}
本的CheckListBox使用 object.ToString()
显示列表中的项目。您可以实现跨所有objects.ToString()搜索,以获得一个项目索引的方法。一旦你的项目索引,你可以叫 SetItemChecked(INT,BOOL);
The checkListBox uses object.ToString()
to show items in the list. You you can implement a method that search across all objects.ToString() to get an item index. Once you have the item index, you can call SetItemChecked(int, bool);
希望它帮助。
这篇关于的CheckedListBox - 搜索由文本项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!