locked
CheckALL function for multiple checkmarks in the panel RRS feed

  • Question

  • Hi,

    I have a lot of checkboxes in the panel in windows application form in C#.  I would like to have CheckAll function that will check all the checkmars.

    I've tried for loop, but it didn't work.

    Any suggestions?

    Thursday, July 12, 2012 6:02 PM

Answers

  • Or to make is simplier, dont need any array to define on a class level, only:

    private void checkBoxCheckAll_CheckChanged(object sender, EventArgs e)
    {
        foreach(CheckBox cb in new CheckBox[]{checkBox1, checkBox2, checkBox3}) //still put all checkBoxes you wanna check into here
             cb.Checked = checkBoxCheckAll.Checked;
    }


    Mitja

    • Marked as answer by rushelp Thursday, July 12, 2012 8:29 PM
    Thursday, July 12, 2012 6:20 PM
  • foreach(CheckBox checkbox in somePanel.Controls.OfType<CheckBox>())
      checkbox.Checked = true;

    Thursday, July 12, 2012 6:33 PM
  • Hi, add new CheckBox control (bellow other, with text: Select all), and do:

    class Form1
    {
        CheckBox[] checkBoxes;
        public Form1()
        {
            //constructor:
            checkBoxes = new CheckBox[]{checkBox1, checkBox2, checkBox3}; //add all into this array        
        }
        
        private void checkBoxCheckAll_CheckChanged(object sender, EventArgs e)
        {
             if(checkBoxCheckAll.Checked)
                 foreach(CheckBox cb in checkBoxes)
                     cb.Checked = true;    //Select all
             else
                  forach(CheckBox cb in checkBoxes)
                      cb.Checked= false;   //Deselct all
        }
    }


    Mitja

    Thursday, July 12, 2012 6:08 PM

All replies

  • Hi, add new CheckBox control (bellow other, with text: Select all), and do:

    class Form1
    {
        CheckBox[] checkBoxes;
        public Form1()
        {
            //constructor:
            checkBoxes = new CheckBox[]{checkBox1, checkBox2, checkBox3}; //add all into this array        
        }
        
        private void checkBoxCheckAll_CheckChanged(object sender, EventArgs e)
        {
             if(checkBoxCheckAll.Checked)
                 foreach(CheckBox cb in checkBoxes)
                     cb.Checked = true;    //Select all
             else
                  forach(CheckBox cb in checkBoxes)
                      cb.Checked= false;   //Deselct all
        }
    }


    Mitja

    Thursday, July 12, 2012 6:08 PM
  • Or to make is simplier, dont need any array to define on a class level, only:

    private void checkBoxCheckAll_CheckChanged(object sender, EventArgs e)
    {
        foreach(CheckBox cb in new CheckBox[]{checkBox1, checkBox2, checkBox3}) //still put all checkBoxes you wanna check into here
             cb.Checked = checkBoxCheckAll.Checked;
    }


    Mitja

    • Marked as answer by rushelp Thursday, July 12, 2012 8:29 PM
    Thursday, July 12, 2012 6:20 PM
  • foreach(CheckBox checkbox in somePanel.Controls.OfType<CheckBox>())
      checkbox.Checked = true;

    Thursday, July 12, 2012 6:33 PM
  • foreach(CheckBox checkbox in somePanel.Controls.OfType<CheckBox>())
      checkbox.Checked = true;

    Lets assume all checkBoxes including the one to check/uncheck all, are in same form (or panel).

    Maybe using anonymous method like:

     - Skip() - to skip that one, or

     - Take() - to take the checkBoxes needed for the job

    ...OfType<CheckBox>().Skip(1))
    {
     
    }
    
    //or
    ...OfType<CheckBox>().Take(numberOfAllCheckBoxesToCheck))
    {
     
    }

    But if checkBoxes are not in order, this will not guarantee you to check all you want to.

    ---

    servy, is there any way to EXCLUDE a particular object (in this case a checkBox control) not to take it into "all.Control.OfType<CheckBox>"?

    So if there is that checkbox to check/uncheck all in the same form (or panel) to exclude it particulary?


    Mitja


    • Edited by Mitja Bonca Thursday, July 12, 2012 6:42 PM
    Thursday, July 12, 2012 6:40 PM
  • servy, is there any way to EXCLUDE a particular object (in this case a checkBox control) not to take it into "all.Control.OfType<CheckBox>"?

    So if there is that checkbox to check/uncheck all in the same form (or panel) to exclude it particulary?

    He asked for code to check all of the checkboxes.  Not a list of pre-defined checkboxes.  Now it's possible for them to be the same (if your pre-defined list happens to contain all of the checkboxes) but that doesn't mean they have to be.  His post doesn't say that he needs to exclude one of the checkboxes, or check some and not others.  If he does, then he has a few options:

    1. Structure the program such that all of the checkboxes that should be checked have the same parent.  Move checkboxes that shouldn't be checked with this group to some other placeholder.
    2. Add a Where clause to filter out checkboxes that meet some condition for being removed.
    3. Follow your logic of a list defined at compile time.

    Note that adding 3 checkboxes to a list isn't that bad, but 10 or 25 starts to get a bit more tedious if it's not actually needed.  Your code also wouldn't work if checkboxes are added/removed dynamically at runtime.

    Thursday, July 12, 2012 7:46 PM
  • Mitja Bonca

    Thank you very much. That works.

    Thursday, July 12, 2012 8:00 PM
  • that's the most simplest code and actually worked !! thanks for this code dude !!

    Wednesday, September 24, 2014 12:54 PM