walkman
walkman

Reputation: 498

CheckedListBox Select All Items - Windows Forms C#

I have a checkbox that when a checked checks all items in a CheckedListBox. When the checkbox goes unchecked it should uncheck all items in the list.

Code:

 if (checkBoxCheckAllPrivileges.Checked)
   for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
      checkedListBoxUsersWhoSee.SetItemChecked(i, true);
 else
   for (int i = 0; i < listBoxUsers.Items.Count; i++)
    checkedListBoxUsersWhoSee.SetItemChecked(i, false);

Is the problem in this code?
Does the .SetitemChecked work giving it the parameter as false?

Is there any other way to uncheck the items?

Upvotes: 4

Views: 3936

Answers (2)

Ab aravind
Ab aravind

Reputation: 1

Use JavaScript Too

$(document).ready(function () {

  $("#<%=checkBoxCheckAllPrivileges.ClientID%>").click(function () {

if ($(this).is(":checked"))

 {

  $("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", true);

  }

 else

 {


$("#<%=checkedListBoxUsersWhoSee.ClientID%> input[type=checkbox]").prop("checked", false);


 }


 });

Upvotes: -1

sowjanya attaluri
sowjanya attaluri

Reputation: 911

You have given wrong item in else part for loop,

if (checkBoxCheckAllPrivileges.Checked)
    for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
        checkedListBoxUsersWhoSee.SetItemChecked(i, true);
else
    for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
        checkedListBoxUsersWhoSee.SetItemChecked(i, false);

Upvotes: 4

Related Questions