Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用StringCollection填充(标记为选中)checkedListBox_C#_Checkedlistbox - Fatal编程技术网

C# 如何使用StringCollection填充(标记为选中)checkedListBox

C# 如何使用StringCollection填充(标记为选中)checkedListBox,c#,checkedlistbox,C#,Checkedlistbox,我在windows窗体上有一个包含10项收藏的checkedListBox。使用C#VS210 我正在寻找一种简单的方法,通过使用Settings.Settings文件中存储的值(存储为System.Collections.Specialized.StringCollection),将checkedListBox中的两个项目标记为选中。我没有找到这个例子,我知道我应该以某种方式使用CheckedListBox.CheckedItems属性,但没有找到一个例子 private void frmUs

我在windows窗体上有一个包含10项收藏的checkedListBox。使用C#VS210

我正在寻找一种简单的方法,通过使用Settings.Settings文件中存储的值(存储为System.Collections.Specialized.StringCollection),将checkedListBox中的两个项目标记为选中。我没有找到这个例子,我知道我应该以某种方式使用CheckedListBox.CheckedItems属性,但没有找到一个例子

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);
    }
}