2 Replies Latest reply on Jan 8, 2010 10:50 AM by arashaga1

    Iterate through the child Objects in a components

    arashaga1

      I have a custom component named myComp that has three TextInputs. And in the main application there is a button that adds this component dynamically to the VBox named myVBox, I would like to know how to iterate through the added components and show the text inside each of the TextInputs in an Alret box. I am pretty new to flex and I couldn't find any examples for it.

       

      Thanks,

        • 1. Re: Iterate through the child Objects in a components
          RK...

          Sample code,

           

          Custom Component


          MyComp1.mxml

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
               <mx:TextInput id="txt1" width="100" />
               <mx:TextInput id="txt2" width="100" />
               <mx:TextInput id="txt3" width="100" />
          </mx:HBox>
          
           
          

           

          Application

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application 
               xmlns:mx="http://www.adobe.com/2006/mxml" 
               layout="absolute"
               creationComplete="init()">
               <mx:Script>
                    <![CDATA[
                         import mx.controls.Alert;
                         import com.MyComp1;
                         
                         private function addComponent(event: MouseEvent): void {
                              var myComp1: MyComp1 = new MyComp1();
                              myVBox.addChild(myComp1);
                         }
                         
                         private function getText(event: MouseEvent): void {
                              var alertMsg: String = "";
                              for (var i: int = 0; i < myVBox.getChildren().length; i++) {
                                   if (myVBox.getChildAt(i) is MyComp1) {
                                        var myComp: MyComp1 = myVBox.getChildAt(i) as MyComp1;
                                        alertMsg += myComp.txt1.text + "\t" +
                                                       myComp.txt2.text + "\t" +
                                                       myComp.txt3.text + " \n"
                                   }
                              }
                              Alert.show(alertMsg);
                         }
                    ]]>
               </mx:Script>
               
               <mx:VBox width="100%" height="100%">
                    <mx:Button label="Add Comp" click="addComponent(event)" />
                    <mx:Button label="Get Value" click="getText(event)" />
                    <mx:VBox id="myVBox" width="100%" height="95%">
                    </mx:VBox>
               </mx:VBox>
          </mx:Application>
          

           

          Hope this helps you

          • 2. Re: Iterate through the child Objects in a components
            arashaga1 Level 1

            Thank you very much!