6 Replies Latest reply on Jun 24, 2008 4:59 PM by FredenFromSweden

    Creating New Fields On The Fly

    FredenFromSweden
      Hey all - trying to set up button, or whatever, to allow the user to add and remove TextInputs on the fly. But, I just can't 'think' of how to do it. I'm thinking a changing variable name for each new field, or some sort of array or object but just can't nail it down.
      Any help?

      thanks,
      M
        • 1. Re: Creating New Fields On The Fly
          peterent2 Adobe Employee
          Let's say you have a simple <mx:Application> (main application file) and you want to add a TextInput field to it. In the function called from the Button's click event handler, do this:

          var newInput:TextInput = new TextInput();
          addChild(newInput);

          Depending on how the container (in this case it is the Application) layout is set you may need to set the newInput.x and inputInput.y properties, too.
          • 2. Re: Creating New Fields On The Fly
            FredenFromSweden Level 1
            Cool - thanks.
            I guess I forgot to mention that I would like it to be multiple fields. I've got the
            var newInput:TextInput = new TextInput();
            addChild(newInput);
            done - but if I want more fields I guess I need to make a new TextInput with a different var name... and that's where I'm having trouble.
            How would I go about making a var with a dynamic name, so I could have newInput1, newInput2, etc…
            Or, should I go about it a different way?

            thanks for the help!
            M
            • 3. Re: Creating New Fields On The Fly
              |>AoA<|_killa
              You could make an array of them(thats how I did it in C#) and there is probably a function somewhere to look up controls according to their id or so.
              • 4. Re: Creating New Fields On The Fly
                peterent2 Adobe Employee
                Make sure you declare your class to be dynamic, this will allow you to add new properties to it on the fly:
                public dynamic class MyComponent extends ...whatever...

                Then you can create new members of the class like this:

                var input:TextInput = new TextInput();
                addChild(input);
                this[ "id"+numChildren ] = input;

                You can also store them in an Array, as well. This is what we do inside of some components.

                --peter
                • 5. Re: Creating New Fields On The Fly
                  levancho Level 3
                  quote:

                  Originally posted by: peterent2
                  Make sure you declare your class to be dynamic, this will allow you to add new properties to it on the fly:

                  --peter

                  not a good idea at all, dynamic classes are performance overhead, and for this problem is not needed at all,
                  instead you have several options : you can either do :

                  for loop (var loopCounter:int ; comparison;loopCounter++) {
                  var ti:TextInput = new TextInput()
                  ti.name = "myName"+loopCounter;
                  this.addChild(ti);
                  }
                  then you can do : this.getChildByName("myName1") to get refference etc ....
                  option 2 :

                  private var storage:Object - new Object () ;
                  var ti:TextInput ;
                  for loop (var loopCounter:int ; comparison;loopCounter++) {
                  ti = new TextInput()
                  storage[("lalala"+loopCounter)] = this.addChild(ti);
                  }
                  then you can get reference to needed inputBox by just doing

                  ti = (storage["lalala1"] as TextInput);


                  • 6. Re: Creating New Fields On The Fly
                    FredenFromSweden Level 1
                    Nice - Nice. That's just what I needed levancho.
                    This is what I came up with. I'm a newbie to Flex/AS so lemme know if there's a better way to do it.
                    Thanks for everyones input!