checkbox array, access through index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aatif
    New Member
    • Mar 2009
    • 28

    checkbox array, access through index

    Hello,
    I have a no. of checkboxes on a c# form application. these are labelled as 1, 2, 3 and so on... I want to programmaticall y make them checked or unchecked according to their label, e-g;

    Code:
    if (i==2)
    //mark checkbox labelled '2' as checked
    I can have a list of checkboxes by filtering myForm.controls[] but don't know how to index them?
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    try this:

    Code:
    foreach(Control cnt in this.Controls)
    {
    
      if(typeof(cnt).ToString().ToLower().Contails("checkbox) )
     {
    if(((CheckBox)cnt).Text.ToString().Equals("2"))
    {
        ((CheckBox)cnt).Checked=true;
        break;
    }
    }
    }
    Last edited by tlhintoq; Jun 1 '09, 02:56 PM. Reason: [CODE] ... your code here [/CODE] tags added

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      For those that don't know how to put in [code] tags
      TIP: When you are writing your question (or reply), there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        May I make a suggestion? As long as you have to make the checkboxes anyway, why not add them to a List<checkbox> while you're making them?

        Comment

        • aatif
          New Member
          • Mar 2009
          • 28

          #5
          Originally posted by tlhintoq
          May I make a suggestion? As long as you have to make the checkboxes anyway, why not add them to a List<checkbox> while you're making them?
          well, then how can I assign their position on the form? some algo to programmaticall y assign them x.y coords according to form size .. :)

          Comment

          • aatif
            New Member
            • Mar 2009
            • 28

            #6
            Originally posted by balame2004
            try this:

            Code:
            foreach(Control cnt in this.Controls)
            {
            
              if(typeof(cnt).ToString().ToLower().Contails("checkbox) )
             {
            if(((CheckBox)cnt).Text.ToString().Equals("2"))
            {
                ((CheckBox)cnt).Checked=true;
                break;
            }
            }
            }
            I m having error on the following line;

            Code:
            typeof(cnt).ToString().....
            I tried this;

            Code:
            if (cnt.GetType() == typeof(System.Windows.Forms.CheckBox))
            but still no success, it doesn't find any checkbox...

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Originally posted by aatif
              well, then how can I assign their position on the form? some algo to programmaticall y assign them x.y coords according to form size .. :)
              You already placed them on the form. Just add them to a List<>

              Code:
              List<checkbox> myChkBx = new List<checkbox>;
              // then down in the form_load event
              myChkBx.Add(checkbox1);
              myChkBx.Add(checkbox2);
              Now you loop through the list.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                When do you want to set the status? When adding them to the form or after some event? If you want to set them while adding the controls then it should be easy to just play around in the loop that creates them (You are creating them in a loop right?).
                If this should happen later in response to some action then write a method that takes the Panel that directly contains these checkboxes and use the later approach that you tried above. It should work then.

                Comment

                • aatif
                  New Member
                  • Mar 2009
                  • 28

                  #9
                  well, after initializing the checkboxes, I want to add them in the list according to their label, e-g the one labeled "1" should be at first index. To make the label-wise marking efficient ...
                  There are 128 or more checkboxes on the form.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    just to clarify, are you making the 128 boxes prgrammatically or through designer? Either way: make one, position it, add it to the list for later reference - THEN worry about initialization

                    If you feel you can't for some reason you might want to share some pertinent code so we can better help

                    Comment

                    • aatif
                      New Member
                      • Mar 2009
                      • 28

                      #11
                      ok, I did it by using nested loops;

                      Code:
                      for (int i = 1; i <= 128; i++)
                      {
                          foreach (Control found in this.groupBox1.Controls)
                          {
                              if (found is CheckBox)
                              {
                                  if (((CheckBox)found).Text.ToString().Equals(i.ToString()))
                                  {
                                      m_CheckBoxList.Add((CheckBox)found);
                                      break;
                                  }
                              }
                          }
                      }
                      then set the conditional status as;

                      Code:
                      m_CheckBoxList[x].Checked = true;

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Couldn't you just do away with one of the loops if you stick to the indexed loop and not use the foreach loop? You could then access each control using the array syntax
                        Code:
                        this.groupBox1.Controls[i]
                        This is one of those situations where the foreach is not appropriate.

                        Comment

                        • aatif
                          New Member
                          • Mar 2009
                          • 28

                          #13
                          Originally posted by r035198x
                          Couldn't you just do away with one of the loops if you stick to the indexed loop and not use the foreach loop? You could then access each control using the array syntax
                          Code:
                          this.groupBox1.Controls[i]
                          This is one of those situations where the foreach is not appropriate.
                          Sorry, I couldn't get you?
                          How would I get them indexed according to the label text?

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Now I don't get you. I thought you said you wanted to check them according to label text rather than index them according to label text.

                            Code:
                            for (int 0 = 1; i < this.groupBox1.Controls.Length; i++) {
                                Control found  = groupBox1.Controls[i];
                                if (found is CheckBox)  {
                                 //get the text label on the checkbox and check it depending on whether
                                 //the label represents an even number or not.     
                                }
                            }

                            Comment

                            • aatif
                              New Member
                              • Mar 2009
                              • 28

                              #15
                              Originally posted by r035198x
                              Now I don't get you. I thought you said you wanted to check them according to label text rather than index them according to label text.
                              I want to index them according to label text, to later check them according to label (index). I needed indexing because they were not initialized sequentially.

                              Comment

                              Working...