1 Reply Latest reply on Jul 24, 2008 2:10 PM by ntsiii

    Accessing components created thru AS addChild

    JennHysuick Level 1
      I am working on an application that requires me to add componts on the fly, based on the field type as entered into the database. (code attached).

      So, I have no idea how many componets will be added, or what types they will be.

      On click of a save button, I need to be able to access the values typed in to the boxes. Normall its textbox.text, but since I don't know the names of the boxes, I'm stumped.

      I have tried adding the name of the component to an array just before I use the .addChild() in the switch statments, but that only gives me an array of the names, and looping through the array, using array(i).text gives me an error.

      Any ideas? I'm stuck on this. Maybe there is a better way of adding the boxes to the page than what I'm doing.


        • 1. Re: Accessing components created thru AS addChild
          ntsiii Level 3
          I strongly urge you to consider using mx:Repeater. It does a lot of work for you , including creating arrays of references to repeated components. If you give some tag in the repeater an id, say mytext", then you can access the instances of that component using mytext[n].text, where "n" is the index of the dataProvider item.

          Doing this below does NOT create an id you can use to reference the component instance:
          myEditor.id = "Answer"+i.toString();

          If you want to stick with manual addChild, you will need to keep a reference to the created components yourself. One effective way is to use an associative array, like a hash table. This is just a dynamic "Object".

          private var _myEditors:Object = new Object();
          myEditor = new RichTextEditor();
          myEditor.text = "Answer question here";
          _myEditors["Answer"+i] = myEditor; //toString is not necessary here

          Then you can later access those instances like this:
          var myEditor:RichTextEditor = _myEditors["Answer"+i];

          But consider Repeater. It does this and much more for you, such as destroying unwanted child components when the data changes.