C# 如何使用StringCollection填充(标记为选中)checkedListBox
我在windows窗体上有一个包含10项收藏的checkedListBox。使用C#VS210 我正在寻找一种简单的方法,通过使用Settings.Settings文件中存储的值(存储为System.Collections.Specialized.StringCollection),将checkedListBox中的两个项目标记为选中。我没有找到这个例子,我知道我应该以某种方式使用CheckedListBox.CheckedItems属性,但没有找到一个例子C# 如何使用StringCollection填充(标记为选中)checkedListBox,c#,checkedlistbox,C#,Checkedlistbox,我在windows窗体上有一个包含10项收藏的checkedListBox。使用C#VS210 我正在寻找一种简单的方法,通过使用Settings.Settings文件中存储的值(存储为System.Collections.Specialized.StringCollection),将checkedListBox中的两个项目标记为选中。我没有找到这个例子,我知道我应该以某种方式使用CheckedListBox.CheckedItems属性,但没有找到一个例子 private void frmUs
private void frmUserConfig_Load(object sender, EventArgs e)
{
foreach (string item in Properties.Settings.Default.checkedListBoxSystem)
{
checkedListBoxSystem.SetItemCheckState(item, CheckState.Checked);
}
}
SetItemCheckState
的第一个参数采用索引(int)。尝试获取要检查的项目的索引,然后使用SetItemCheckState
和索引一起检查它。如何使用
丹尼,这个例子非常有效。谢谢你的回复。我还学习了扩展方法。最好的,约翰。
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;
}
}
}
}
private void frmUserConfig_Load(object sender, EventArgs e)
{
foreach (string item in Properties.Settings.Default.checkedListBoxSystem)
{
checkedListBoxSystem.SetChecked(item);
}
}