我在windows窗体上的收藏集中有一个包含10个项目的checkedListBox。使用C# VS210。
我正在寻找一种简单的方法,通过使用存储在checkedListBox文件(存储为System.Collections.Specialized.StringCollection). )中的值,将我的Settings.Settings中的两个项目标记为选中我一直没有找到这个例子,我知道我应该以某种方式使用CheckedListBox.CheckedItems属性,但还没有找到一个例子。
private void frmUserConfig_Load(object sender, EventArgs e)
{
foreach (string item in Properties.Settings.Default.checkedListBoxSystem)
{
checkedListBoxSystem.SetItemCheckState(item, CheckState.Checked);
}
}发布于 2012-12-13 13:08:06
使用Extension method怎么样?
static class CheckedListBoxHelper
{
public static void SetChecked(this CheckedListBox list, string value)
{
for (int i = 0; i < list.Items.Count; i++)
{
if (list.Items[i].Equals(value))
{
list.SetItemChecked(i, true);
break;
}
}
}
}并稍微更改load事件中的逻辑,如下所示:
private void frmUserConfig_Load(object sender, EventArgs e)
{
foreach (string item in Properties.Settings.Default.checkedListBoxSystem)
{
checkedListBoxSystem.SetChecked(item);
}
}发布于 2012-12-13 11:16:09
SetItemCheckState的第一个参数接受索引(int)。尝试获取要检查的项目的索引,然后对索引使用SetItemCheckState进行检查。
https://stackoverflow.com/questions/13851829
复制相似问题