4

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

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

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

谢谢

4

2 回答 2

6

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);
}
于 2010-02-01T23:58:10.607 回答
3

如果您想在添加项目后执行此操作,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;
}
于 2010-02-02T00:00:51.647 回答