1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problems with unchecking "Select All" checkbox by code

Discussion in 'C#' started by joolz, Jul 22, 2010.

  1. joolz

    joolz New Member

    Jul 22, 2010
    Likes Received:
    Trophy Points:
    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);
    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
    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!

Share This Page