如何在C#的列表框中插入一个复选框?

ady*_*ady 2 c# checkbox listbox winforms

我想要一个代码在c sharp中的列表框中插入一个复选框.在选中复选框时,必须选择列表框中的所有项目.

JK.*_*JK. 6

您可以使用CheckListBox显示列表,每个项旁边都有一个复选框.

但要创建一个选择列表中所有内容的复选框,它必须位于列表框之外(上方或下方或旁边).然后你可以使用如下代码:

public void SelectAllCheckBox_CheckedChanged(object s, EventArgs e) 
{
    foreach (var item in ListBox1.Items) 
    {
        item.Selected = SelectAllCheckBox.Checked;
    }
}
Run Code Online (Sandbox Code Playgroud)

没有控件在列表中有一个复选框:例如,这就是你的意思:

+----------------------------------------+
| [x] Select All                         |
| Item one                               |
| Item two                               |
| Item three                             |
| Item four                              |
| Item five                              |
+----------------------------------------+
Run Code Online (Sandbox Code Playgroud)

相反,您必须使用两个控件:复选框和单独的列表框:

[x] Select All                         

+----------------------------------------+
| Item one                               |
| Item two                               |
| Item three                             |
| Item four                              |
| Item five                              |
+----------------------------------------+
Run Code Online (Sandbox Code Playgroud)