在 WinForms 中迭代 CheckedListBox?

发布于 2024-08-19 22:08:03 字数 250 浏览 5 评论 0原文

如果我在 Win 表单中有选中的列表框,我像这样填写

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name);

如何迭代tasksCheckedListBox.Items 并将一些复选框设置为选中?

谢谢

If I have Checked list box in Win forms which I fill like this

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name);

How can I iterate tasksCheckedListBox.Items and set some check boxes as checked?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-08-26 22:08:03

add 方法采用可选的 IsChecked 参数。然后,您可以将对象以正确的状态添加到选中的列表框中。

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name, isChecked);

或者,您可以在添加项目后更改项目的选中状态,如下所示:

foreach(var task in tasks)
{
    tasksCheckedListBox.SetItemChecked(clb.Items.IndexOf(task), isChecked);
}

The add method takes an optional IsChecked parameter. You can then add your objects into the checked list box in the correct state.

List<Tasks> tasks = db.GetAllTasks();
        foreach (var t in tasks)
            tasksCheckedListBox.Items.Add(t.Name, isChecked);

Or you can change the checked state of an item after you add it with something like this:

foreach(var task in tasks)
{
    tasksCheckedListBox.SetItemChecked(clb.Items.IndexOf(task), isChecked);
}
萌吟 2024-08-26 22:08:03

如果您想在添加项目后执行此操作,有一个示例 在 MSDN

复制到这里:

private void CheckEveryOther_Click(object sender, System.EventArgs e) {
    // Cycle through every item and check every other.

    // Set flag to true to know when this code is being executed. Used in the ItemCheck
    // event handler.
    insideCheckEveryOther = true;

    for (int i = 0; i < checkedListBox1.Items.Count; i++) {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0) {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                checkedListBox1.SetItemChecked(i, true);
        }
    }        

    insideCheckEveryOther = false;
}

If you want to do it after the items have been added, there is an example on MSDN

Copied here:

private void CheckEveryOther_Click(object sender, System.EventArgs e) {
    // Cycle through every item and check every other.

    // Set flag to true to know when this code is being executed. Used in the ItemCheck
    // event handler.
    insideCheckEveryOther = true;

    for (int i = 0; i < checkedListBox1.Items.Count; i++) {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0) {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                checkedListBox1.SetItemChecked(i, true);
        }
    }        

    insideCheckEveryOther = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文