您可以使用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)