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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:52:10  来源:igfitidea点击:

How to programmatically check an item in a CheckedListBox in C#?

c#winformscheckedlistbox

提问by Andrew Ducker

I have a CheckedListBox, and I want to automatically tick one of the items in it.

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

The CheckedItemscollection doesn't allow you to add things to it.

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

Any suggestions?

有什么建议?

采纳答案by Jon Skeet

You need to call SetItemCheckedwith the relevant item.

您需要致电SetItemChecked相关项目。

The documentation for CheckedListBox.ObjectCollectionhas an example which checks every other item in a collection.

文档CheckedListBox.ObjectCollection有一个示例,它检查集合中的所有其他项目。

回答by B. Clay Shannon

This is how you can select/tick or deselect/untick all of the items at once:

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

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

回答by ePandit

Suppose you want to check the item on clicking a button.

假设您想在单击按钮时检查该项目。

private void button1_Click(object sender, EventArgs e)
{
    checkedListBox1.SetItemChecked(itemIndex, true);
}

Where itemIndex is the index of the item to be checked, it starts from 0.

其中 itemIndex 是要检查的项的索引,从 0 开始。

回答by Adiii

Use:

用:

string[] aa = new string[] {"adiii", "yaseen", "salman"};
foreach (string a in aa)
{
    checkedListBox1.Items.Add(a);
}

Now code where you want to check all:

现在编码您要检查的所有内容:

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

To uncheck all:

要取消选中所有:

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

回答by Ondrej Jáno?ík

In my program I've used the following trick:

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

CheckedListBox.SetItemChecked(CheckedListBox.Items.IndexOf(Item),true);

How does things works:
SetItemChecked(int index, bool value) is method which sets the exact checked state at the specific item. You have to specify index of item You want to check (use IndexOf method, as an argument specify text of item) and checked state (true means item is checked, false unchecked).
This method runs through all items in CheckedListBox and checks (or unchecks) the one with specified index.
For example, a short piece of my code - FOREACH cycle runs through specified program names, and if the program is contained in CheckedLitBox (CLB...), checks it:

工作原理:
SetItemChecked(int index, bool value) 是在特定项目上设置确切检查状态的方法。您必须指定要检查的项目的索引(使用 IndexOf 方法,作为参数指定项目的文本)和检查状态(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);
}

回答by Konstantin Samsonov

I use an extension:

我使用扩展名:

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