在CheckedListBox上运行并检查文件中的内容
本文关键字:文件 检查 CheckedListBox 运行 | 更新日期: 2023-09-27 18:13:41
我有一个CheckedListBox,包含:
AA
BB
CC
DD
我有一个字符串包含:CC
我需要在CheckedListBox中选中CC
如果您想在添加项目后执行此操作,MSDN上有一个示例
private void CheckEveryOther_Click(object sender, System.EventArgs e) {
// Cycle through every item and check every other.
// Set flag to true to know when this code is being executed. Used in the ItemCheck
// event handler.
insideCheckEveryOther = true;
for (int i = 0; i < checkedListBox1.Items.Count; i++) {
// here you need to compare with that string....
checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
else
checkedListBox1.SetItemChecked(i, true);
}
}
insideCheckEveryOther = false;
}
遍历checkListBox。对于每个项目,使用myStr.Contains(item. tostring())在字符串中搜索项目,并根据包含结果将项目标记为已选中或未选中。
string myStr = "CC";
for (int it = 0; it < checkedListBox1.Items.Count; it++)
{
checkedListBox1.SetItemChecked(
it, myStr.Contains(checkedListBox1.Items[it].ToString()));
}