3 Replies Latest reply on May 11, 2010 1:03 AM by MrFork

    Accessing Children of VBox

    MrFork

      Hello All,

       

      I have dynmaically created some CheckBox's and added them to a VBOX.

       

      What i want to do is create a function that will iterate through them all and deselect. I have got this far:

       

      private function clearSelection():void
              {
                  for (var i:int = 0; i < campaignRadioButtons.numChildren; i++) {
                      var tmpCheckBox:CheckBox = new CheckBox
                      tmpCheckBox = CheckBox(campaignRadioButtons.getChildAt(i));
                      trace (tmpCheckBox.label);
                  }
              }

       

      Ok so this at the moment returns all of the labels. What I want to do is get access to the 'selection' attrubute and set it to false. How do I do this?.. without having to re add them all over again.

       

      Thanks

        • 1. Re: Accessing Children of VBox
          David_F57 Level 5

          hi,

           

          there is a couple of ways you can do this but I think putting your checkboxes into an array makes it easier to reference them.

           

          David.

           

          private var cb_arr:Array = new Array();

           

          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);

          cb_arr.push(cb);

          }

          }

           

           

          private function setCheckBoxes(): void

          {

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

          {

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

          }

          }

          • 2. Re: Accessing Children of VBox
            GordonSmith Level 4

            private function clearSelection():void
            {

                var n:int = campaignRadioButtons.numChildren;
                for (var i:int = 0; i < n; i++)

                {
                    var tempCheckBox:CheckBox = CheckBox(campaignRadioButtons.getChildAt(i));
                    tempCheckBox.selected = false;
                }
            }

             

            Note: DON'T do

             

            private function clearSelection():void

            {

                var n:int = campaignRadioButtons.numChildren;
                for (var i:int = 0; i < n; i++)

                {
                    var tempCheckBox:CheckBox = new CheckBox();

                    tempCheckBox = CheckBox(campaignRadioButtons.getChildAt(i));

                    tempCheckBox.selected = false;

                }

            }

             

            This needlessly creates n new CheckBox instances and frees them when the reference to the ith new CheckBox in tempCheckBox is overwritten with the reference to the ith existing CheckBox.

             

            Gordon Smith

            Adobe Flex SDK Team

            • 3. Re: Accessing Children of VBox
              MrFork Level 1

              Thank You