C# 如何通过单击检查 CheckListBox 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1503305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check CheckListBox item with single click?
提问by Pratik Deoghare
I am coding Windows Forms
application in C# and using CheckListBox
Control.
我正在Forms
用 C#编写 Windows应用程序并使用CheckListBox
Control。
How to check CheckListBox item with just single click?
如何通过单击检查 CheckListBox 项目?
采纳答案by rahul
I think you are looking for
我想你正在寻找
CheckOnClickproperty
set it to true
将其设置为 true
Gets or sets a value indicating whether the check box should be toggled when an item is selected.
获取或设置一个值,该值指示在选择项目时是否应切换复选框。
回答by Adiii
you can also check all by button click or click on checklist
您还可以通过单击按钮或单击清单来检查所有内容
private void checkedListBox1_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
checkedListBox1.SetItemChecked(i, true);
}
回答by daniele3004
回答by Scope Creep
I just finished working through an issue where I had set CheckOnClick to True via the designer, but the UI was still requiring a second click to check items. What I found is that for whatever reason, the designer file was not updating when I changed the value. To resolve, I went into the designer file and added a line
我刚刚完成了一个问题,我通过设计器将 CheckOnClick 设置为 True,但 UI 仍然需要再次单击以检查项目。我发现无论出于何种原因,当我更改值时,设计器文件没有更新。为了解决,我进入了设计器文件并添加了一行
this.Product_Group_CheckedListBox.CheckOnClick = true;
After this, it worked as expected. Not sure why the designer didn't update, but maybe this workaround will help someone.
在此之后,它按预期工作。不知道为什么设计师没有更新,但也许这个解决方法会对某人有所帮助。
回答by tjmaher
You can also use a check box exterior to the CheckListBox to check/uncheck all items. On the same form add a checkbox near the CheckedListBox and name it CkCheckAll. Add the Click event for the CheckBox (which I prefer to the CheckChanged event). There is also a button (BtnAdd) next to the CheckedListBox which will add all checked items to a database table. It is only enabled when at least one item in the CheckedListBox is checked.
您还可以使用 CheckListBox 外部的复选框来选中/取消选中所有项目。在同一个表单上,在 CheckedListBox 附近添加一个复选框,并将其命名为 CkCheckAll。为 CheckBox 添加 Click 事件(我更喜欢 CheckChanged 事件)。CheckedListBox 旁边还有一个按钮 (BtnAdd),它将所有选中的项目添加到数据库表中。仅当 CheckedListBox 中的至少一项被选中时才启用。
private void CkCheckAll_Click(object sender, EventArgs e)
{
CkCheckAll.Text = (CkCheckAll.Checked ? "Uncheck All" : "Check All");
int num = Cklst_List.Items.Count;
if (num > 0)
{
for (int i = 0; i < num; i++)
{
Cklst_List.SetItemChecked(i, CkCheckAll.Checked);
}
}
BtnAdd_Delete.Enabled = (Cklst_List.CheckedItems.Count > 0) ? true : false;
}