简体   繁体   English

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

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

I have a CheckedListBox, and I want to automatically tick one of the items in it. 我有一个CheckedListBox,我想自动勾选其中的一个项目。

The CheckedItems collection doesn't allow you to add things to it. CheckedItems集合不允许您向其添加内容。

Any suggestions? 有什么建议?

You need to call SetItemChecked with the relevant item. 您需要使用相关项调用SetItemChecked

The documentation for CheckedListBox.ObjectCollection has an example which checks every other item in a collection. CheckedListBox.ObjectCollection文档有一个示例,它检查集合中的每个其他项。

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

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. SetItemChecked(int index,bool value)是在特定项目处设置精确检查状态的方法。 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 ). 您必须指定要检查的项目的索引( 使用IndexOf方法,作为参数指定项目的文本 )和选中状态( true表示项目已选中,false未选中 )。
This method runs through all items in CheckedListBox and checks ( or unchecks ) the one with specified index. 此方法遍历CheckedListBox中的所有项,并检查( 或取消选中 )具有指定索引的项。
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: 例如,我的一小段代码 - 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);
}

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开始。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM