C# 如何在C中以编程方式检查CheckedListBox中的项?

C# 如何在C中以编程方式检查CheckedListBox中的项?,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,我有一个CheckedListBox,我想自动勾选其中一个项目 CheckedItems集合不允许您向其中添加内容 有什么建议吗?您需要打电话询问相关项目 有一个示例,用于检查集合中的所有其他项目。这是您可以一次选择/勾选或取消选择/取消勾选所有项目的方式: private void SelectAllCheckBoxes(bool CheckThem) { for (int i = 0; i <= (checkedListBox1.Items.Count - 1); i++) {

我有一个CheckedListBox,我想自动勾选其中一个项目

CheckedItems集合不允许您向其中添加内容

有什么建议吗?

您需要打电话询问相关项目


有一个示例,用于检查集合中的所有其他项目。

这是您可以一次选择/勾选或取消选择/取消勾选所有项目的方式:

private void SelectAllCheckBoxes(bool CheckThem) {
    for (int i = 0; i <= (checkedListBox1.Items.Count - 1); i++) {
        if (CheckThem)
        {
            checkedListBox1.SetItemCheckState(i, CheckState.Checked);
        }
        else
        {
            checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
        }
    }  
}

假设您要在单击按钮时检查项目

private void button1_Click(object sender, EventArgs e)
{
    checkedListBox1.SetItemChecked(itemIndex, true);
}
其中itemIndex是要检查的项目的索引,它从0开始。

使用:

string[] aa = new string[] {"adiii", "yaseen", "salman"};
foreach (string a in aa)
{
    checkedListBox1.Items.Add(a);
}
现在,在要检查的位置编写代码:

private void button5_Click(object sender, EventArgs e)
{
    for(int a=0; a<checkedListBox1.Items.Count; a++)
        checkedListBox1.SetItemChecked(a, true);
}
private void button_Click(object sender, EventArgs e)
{
    for(int a=0; a<checkedListBox1.Items.Count; a++)
        checkedListBox1.SetItemChecked(a, false);
}
要取消选中全部,请执行以下操作:

private void button5_Click(object sender, EventArgs e)
{
    for(int a=0; a<checkedListBox1.Items.Count; a++)
        checkedListBox1.SetItemChecked(a, true);
}
private void button_Click(object sender, EventArgs e)
{
    for(int a=0; a<checkedListBox1.Items.Count; a++)
        checkedListBox1.SetItemChecked(a, false);
}

在我的程序中,我使用了以下技巧:

CheckedListBox.SetItemChecked(CheckedListBox.Items.IndexOf(Item),true);
事情是如何运作的: SetItemCheckedint index,bool value是一种方法,用于设置特定项的精确检查状态。必须使用IndexOf方法指定要检查的项的索引,因为参数指定项的文本,并且checked state true表示项已选中,false表示项未选中。 此方法将运行CheckedListBox中的所有项,并选中或取消选中具有指定索引的项。 例如,我的一小段代码-FOREACH循环通过指定的程序名运行,如果该程序包含在CheckedLitBox CLB…中,请检查它:

string[] ProgramNames = sel_item.SubItems[2].Text.Split(';');
foreach (string Program in ProgramNames)
{
    if (edit_mux.CLB_ContainedPrograms.Items.Contains(Program))
        edit_mux.CLB_ContainedPrograms.SetItemChecked(edit_mux.CLB_ContainedPrograms.Items.IndexOf(Program), true);
}
我使用一个扩展名:

public static class CheckedListBoxExtension
{
    public static void CheckAll(this CheckedListBox listbox)
    {
        Check(listbox, true);
    }

    public static void UncheckAll(this CheckedListBox listbox)
    {
        Check(listbox, false);
    }

    private static void Check(this CheckedListBox listbox, bool check)
    {
        Enumerable.Range(0, listbox.Items.Count).ToList().ForEach(x => listbox.SetItemChecked(x, check));
    }
}

您不需要if/else条件,在for循环中,您可以按如下方式执行:checkedListBox1.SetItemCheckedi,checkTheme;对于那些想知道的人,有相反的GetItemChecked,因此您可以通过调用list.SetItemCheckedi来反转选择!list.GetItemCheckedi;当我调用SetItemChecked0时,如果为true,那么GetItemChecked0仍然返回false@马塔诺:至少听起来很不寻常。我建议你问一个新问题。嗯。。。这是最短的D