Bre*_*lyn 24 c# checkbox button winforms checklistbox
我想要一个按钮,一旦点击,它将选中我的核对表框中的所有复选框.我搜索了可能的答案,但我总是看到asp.net和javascript的例子.我在c#中使用Windows表单.谢谢你的回复.
小智 54
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
Run Code Online (Sandbox Code Playgroud)
在多次遇到这个问题后,我决定用扩展方法自己一劳永逸地解决它。
public static class Extensions
{
public static void CheckAll(this CheckedListBox checkedListBox, bool check)
{
for (int i = 0; i < checkedListBox.Items.Count; i++)
checkedListBox.SetItemChecked(i, check);
}
}
Run Code Online (Sandbox Code Playgroud)
MyCheckedListBox.CheckAll(true);
Run Code Online (Sandbox Code Playgroud)
从C#后面的代码中调用一个方法并编写这段代码,然后您就可以检查/取消选中它们。这会选中或取消选中复选框列表中存在的所有复选框。希望它会有所帮助。
foreach (ListItem item in CheckBoxList.Items)
{
item.Selected = true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63451 次 |
| 最近记录: |