如何单击检查 CheckListBox 项?

我正在用 C # 编写 Windows Forms应用程序,并使用 CheckListBox控件。

如何单击检查 CheckListBox 项目?

49517 次浏览

I think you are looking for

CheckOnClick property

set it to true

Gets or sets a value indicating whether the check box should be toggled when an item is selected.

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);


}

Set the property at Design Time in this way

enter image description here

or by code:

CheckedListBox.CheckOnClick = true;

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

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.

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.

    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;
}