Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 622
walkman Avatar asked Oct 27 '25 05:10

walkman


1 Answers

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);
like image 76
sowjanya attaluri Avatar answered Oct 29 '25 20:10

sowjanya attaluri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!