3 Replies Latest reply on May 7, 2010 8:10 AM by David_F57

    Referencing checkboxes using strings of their IDs

    Chris Callaghan

      I'm sure this is a really easy one (im a beginner)...

       

      I'm creating checkboxes at runtime and assigning IDs in the format "checkBox_1", "checkBox_2" etc.

       

      I'm also pushing these strings to an array, so I have a list of all the checkboxes I have made (across multiple NavigationContent containers)

       

      I need to loop through the array and convert the string into a valid checkbox reference and grab the label of that checkbox.

       

      What I'm struggling with is that the following throws an error

       

       

      var myCheckBox:CheckBox;

      myCheckBox = CheckBox("checkBox_1");

      trace(myCheckBox.label);

       

       

      and myCheckBox = "checkBox_1" as CheckBox; doesnt work

       

       

      I'm missing something really basic here

        • 1. Re: Referencing checkboxes using strings of their IDs
          Sree_D Level 2

          Try adding the checkbox item to the array instead of storing the Ids.

           

          If you want to store only ids then used get child methods to get the check box item.

          1 person found this helpful
          • 2. Re: Referencing checkboxes using strings of their IDs
            Alex Browning

            Unless you want to store the IDs in an array for a specific reason (in which case the advice above is valid - push the checkbox object to the array instead of just the ID) you can just loop through all the checkboxes on the canvas by doing something like this :-

             

            for each (var myCheckBox:CheckBox in yourContainer.getChildren())

            {

                 trace(myCheckBox.label.toString());

            }

             

            Hope that helps,

             

            Alex.

            • 3. Re: Referencing checkboxes using strings of their IDs
              David_F57 Level 5

              hi,

               

              Pushing the object onto the array is the better option but you can also reference the object by name, either way you need to typecast any operations on the checkbox , If you decide to use the checkbox's name you need to retreive the displayobject first then you can use it as per the call to set the selected value shown below.

               

              David.

               

              protected function application1_creationCompleteHandler(event:FlexEvent):void

              {

              for (var i:int=1; i < 11;i++)

              {

              var cb:CheckBox = new CheckBox();

              cb.x=10;

              cb.y=25*i+10;

              cb.selected = false;

              cb.label = "Check no:"+String(i);

              cbCanvas.addChild(cb);

              arr.push(cb.name);

              //arr.push(cb);

              }

              }

               

               

              private function setCheckBoxes(): void

              {

              var tmpCB:DisplayObject;

              for (var i:int=0; i <arr.length;i++)

              {

              tmpCB = cbCanvas.getChildByName(arr[i]);

              (tmpCB as CheckBox).selected=true;

              //(arr[i] as CheckBox).selected=true;

              }

              }