C# 如何以编程方式设置 winforms Checkedlistbox 中所有项目的选中状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13062064/
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-10 01:31:25  来源:igfitidea点击:

How do I set the checked state of all items in a winforms Checkedlistbox programmatically?

c#winforms

提问by Hardik

I am working on a Windows form Application. I want to check/uncheckall checkboxes in checkedlistbox.

我正在处理 Windows 窗体应用程序。我想要check/uncheck检查列表框中的所有复选框。

I am using following code to generate checkboxes dynamically.

我正在使用以下代码动态生成复选框。

    var CheckCollection = new List<CheckedBoxFiller>();
    foreach (DataRow dr in dt.Rows)
        CheckCollection.Add(new CheckedBoxFiller {
                                Text = dr["ImageName"].ToString(),
                                Value = dr["ImageId"].ToString()
        });
    chklbEvidenceTags.DataSource = CheckCollection;
    chklbEvidenceTags.DisplayMember = "Text";
    chklbEvidenceTags.ValueMember = "Value";

And this is the CheckboxFiller class

这是 CheckboxFiller 类

private class CheckedBoxFiller {
    public string Text { get; set; }
    public string Value { get; set; }
}

Now I want to check/Uncheckall checkboxes. How can I achieve this?

现在我想check/Uncheckall checkboxes。我怎样才能做到这一点?

Any help would be useful.

任何帮助都会有用。

采纳答案by Hardik

I found solution.

我找到了解决方案。

 for (int i = 0; i < chklistbox.Items.Count; i++)
      chklistbox.SetItemCheckState(i, (state ? CheckState.Checked : CheckState.Unchecked));

stateis boolenvalue.

stateboolen价值。

回答by brsfan

If you have a large Items list, this approach may be a little more efficient for uncheckingitems. It requires you only cycle through the items that are actually checked:

如果您有一个很大的 Items 列表,这种方法对于取消选中项目可能会更有效一些。它要求您只循环查看实际检查的项目:

    private void UncheckAllItems()
    {
        while (chklistbox.CheckedIndices.Count > 0)
            chklistbox.SetItemChecked(chklistbox.CheckedIndices[0], false);
    }

And if you use multiple CheckedListBox controls throughout your project and wanted to take it a step further, it could be added as an extension method:

如果您在整个项目中使用多个 CheckedListBox 控件并希望更进一步,可以将其添加为扩展方法:

    public static class AppExtensions
    {
        public static void UncheckAllItems(this System.Windows.Forms.CheckedListBox clb)
        {
            while (clb.CheckedIndices.Count > 0)
                clb.SetItemChecked(clb.CheckedIndices[0], false);
        }
    }

Extension method call:

扩展方法调用:

    chklistbox.UncheckAllItems();

回答by ramesh choudhury

To Uncheck/Check all listItems Do the below Code:

要取消选中/选中所有列表项,请执行以下代码:

 boolean state =false;//False->Uncheck,true->Check

 for (int i = 0; i < chklistbox.Items.Count; i++)
      chklistbox.SetItemCheckState(i, (state ? CheckState.Checked : CheckState.Unchecked));

回答by Anjan Kant

Check/Uncheck all list items written below code:

选中/取消选中以下代码编写的所有列表项:

if (checkBox1.Checked)
    {
        for (int i = 0; i < chkboxlst.Items.Count; i++)
        {
            chkboxlst.SetItemChecked(i, true);
        }
    }
    else
    {
        for (int i = 0; i < chkboxlst.Items.Count; i++)
        {
            chkboxlst.SetItemChecked(i, false);
        }
    }