Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Problems with unchecking "Select All" checkbox by code (http://www.go4expert.com/forums/unchecking-select-checkbox-code-t22787/)

joolz 22Jul2010 17:25

Problems with unchecking "Select All" checkbox by code
 
Dear all, this might be a stupid question but I'm a newbie to C# and cannot work out how to do this correctly. Basically I have done the following:

1. A listview, with checkboxes enabled, which has a lv_ItemChecked event. The code tries to check if lv.CheckedIems.Count is equal to lv.Items.Count, then obviously that means all the items have been selected and I should check the "Select All" by code as well. So I go like this:

chkSelectAll.Checked = lv.CheckedItems.Count.Equals(lv.Items.Count);

Basically this will also uncheck the Select All checkbox if not all the checkboxes have been ticked.

2. A checkbox "Select All" which when checked, will select all checkboxes in the listview, and when unchecked, will deselect those checkboxes. It does this by an event of chkSelectAll_CheckedChanged. The code goes like this:

if (chkSelectAll.Checked.Equals(false))
{
mnuClearAll_Click(sender, e);
}
else
{
mnuSelectAll_Click(sender, e);
}

Where obviously the menu commands do a foreach and (un)check the checkboxes in the listview.

*** The problem ***

As you experienced coders might already have noticed, this presents a problem when I have all checkboxes checked and I try to uncheck one of the boxes. As soon as that happens, it unchecks, by code, the Select All checkbox... triggering the chkSelectAll_CheckedChanged event, which in turn also triggers the mnuClearAll_Click event.

In other words as soon as I deselect one checkbox, the entire list gets unchecked... I cannot work out how to get this right... I assume there must be something I can use in the sender and the EventArg as in
lv_ItemChecked(
object sender, EventArgs e) but I cannot find info on how to use those properly...

It'd be tremendously helpful if you could help me out here... Many thanks!


All times are GMT +5.5. The time now is 14:24.