10 Replies Latest reply on Oct 15, 2010 8:25 AM by Devtron

    Working with TextInput boxes

    Alan321977

      Hello,

       

      Wondering if someone can help me with a problem i'm having.  I am trying to set the text property of a number of TextInput boxes to blank.

       

      I used Flash Builder's design mode to layout my user interface so it has a Panel with 9 TextInput boxes.

       

      I have named each TextInput box "d01" to "d09" so that when the user clicks the clear button it will do the following:

       

      d01.text = "";

      d02.text = "";

       

      etc...

       

      This code is ok for a couple of boxes but I would rather use a loop as the final version will have 100+ TextInputs.

       

      I can't seem to work out how to reference each TextInput instance without doing it individually.

       

       

      Any help would be great.

       

      Thanks

      Alan

        • 1. Re: Working with TextInput boxes
          Claudiu Ursica Level 4

          You mean you actually add 100+ TextInputs by hand?

          You create the inside a for loop give the id property an id an the current

          iteration numeber and assign the text property to "";

           

          var i:uint;

          for (i = 0; i < yourNumberOfThextInputs; i++)

          {

              var tmp:TextInput = new TextInput();

              tmp.name = "txtInput" + String(i);

              //add listeners do whatever here

           

             addChild(tmp);

             //addElement(tmp);

          }

           

           

          You should place this inside the override of the createChildren of the parent

          component.

           

          When you need an input you can get it by using getChildByName(name:String). In

          case you know the index of the component and used addElement() e.g. for groups

          or other FX4 descendants you can try getElementAt(index)...

           

          HTH,

          C

          1 person found this helpful
          • 2. Re: Working with TextInput boxes
            Alan321977 Level 1

            Thanks for the reply,

             

            Yes, I drag and drop them to the state so I can see how the layout will look with everything on screen.

             

            I'll give your code a try.

             

            Many thanks

            Alan

            • 3. Re: Working with TextInput boxes
              Devtron Level 3

              "You mean you actually add 100+ TextInputs by hand?"

               

              ^ Statements like this baffle me.

               

              How else are you supposed to do it? All through ActionScript? I'd rather hang myself than to provide all that layout at runtime. Not only will you be creating the 100 textboxes, you will also need to handle all their configuration, layout, styling and functionality DYNAMICALLY.

               

              So which is crazier? Tons of ActionScript or Tons of MXML?

               

              Anyone who is adding 100 textboxes EASILY at runtime through ActionScript must not have any real layout or design going on in their application. Right?

               

              I mean, sure, it's easy to generate 100 textboxes in a container. But how do you set all the properties and stuff you NEED to build business applications, like "restrict", "maxChars", events, color, id, value,style, editable, etc.

               

              Doing it through ActionScript seems like a ton of more work than writing 100 textboxes in MXML.

               

              I would suggest NOT using design mode, but using copy/paste in MXML. That's what I do.

              • 4. Re: Working with TextInput boxes
                Claudiu Ursica Level 4

                "Anyone who is adding 100 textboxes at runtime through ActionScript must  not

                have any real layout or design going on in their application. Right?"

                 

                "I used Flash Builder's design mode to layout my user interface so it has a

                Panel with 9 TextInput boxes."

                 

                 

                So I guess you're right. Since there's a panel involved we talk about vertical,

                horizontal or absolute layout. And they come out out of the box. Seems only

                normal to add dynamically. Need more complex layout logic go Flex4 with custom

                layout . The thing is you do it inside a for loop and you only do it once, not

                100 times. You can still handle all the  "restrict", "maxChars", events, color,

                stykle, editable, etc from the code.

                 

                If you talk about 10-15 controls do it by hand, otherwise go for the code...

                 

                My 2 cents,

                C

                • 5. Re: Working with TextInput boxes
                  Devtron Level 3

                  But wouldnt it be a little bit more work to have to configure all those attributes in the for loop?

                   

                  I mean think about it...in a real world business environment...you would need to validate stuff like: email address, password, phone numbers, dates, etc.

                   

                  You seriously recommend doing it in a for loop?

                  • 6. Re: Working with TextInput boxes
                    Claudiu Ursica Level 4

                    I don't think there is one solution fits them all. Even though I don't remember

                    when I use last time the design view. I work mostly with the code view. However

                    you rarely get 100 childs inside a component. Email address, password, phone

                    numbers, dates sounds more like a regular form up to 10 items where you can

                    easily do it in mxml, and put validators and other things. I hope I did not

                    leave the impression I have something against design view. I use mxml a lot but

                    with the code view. My point is if you have a lot of components is highly

                    unlikely there is much design in there anyway. For the situation in this thread

                    I would use a loop...

                     

                    I have named each TextInput box "d01" to "d09" so that when the user clicks the

                    clear button it will do the following:

                     

                    d01.text = "";

                    d02.text = "";

                    now if you hold reference to those objects and they have some sort of index

                    attached (01, 02), that sounds like looping.

                     

                     

                    Other situations I probably handle them different.

                     

                    C

                    • 7. Re: Working with TextInput boxes
                      Alan321977 Level 1

                      As expected the code worked and I tried it on the application I'm working on.  However, I'm still not certain why you can loop through existing TextInputs that were arranged using design mode.  I switch back and forth between design and source modes but I can see at a certain point why you would only work in source mode. 

                       

                      I think your both right and one solution won't fit all. Maybe I'm just not seeing it, but why can't I use a similar method to manipulate TextInputs that have not been created at runtime? 

                       

                      If it is not possible to loop through TextInputs that were created in design mode, is that not a failing with this particular language? (not got a lot of experience with Actionscript)

                       

                      So far the only way I can get it to work in that loop is by creating them at runtime..but it would be nice to do it the other way also as I didn't really want 1 line of Actionscript code for each TextInput.

                       

                      Thanks

                      Alan

                      • 8. Re: Working with TextInput boxes
                        Claudiu Ursica Level 4

                        Bu you can loop through them. You can push their id's inside an Vector and loop

                        through Vector. You can assign names to them also in design mode.

                         

                         

                        C

                        • 9. Re: Working with TextInput boxes
                          Alan321977 Level 1

                          Thats seems to be the issue I'm having at the moment.

                           

                          I am unsure how to reference the existing TextInputs without having a line of code for each one. 

                           

                           

                          I can see how to do it at runtime but that always refers to a new TextInput();, i need to refer to existing text inputs.

                           

                          var ti:TextInput = new TextInput();
                          var tiArray:Array = new Array();
                          var i:uint;
                                              
                          for (i = 7; i<100; i++)
                          {
                               if (i <10)
                               {
                                    ti.id = "d0" + String(i);     
                                    tiArray.push(ti);               
                                    trace (ti.id);     
                               }
                               else
                               {
                                    ti.id = "d" + String(i);               
                                    tiArray.push(ti);               
                                    trace (ti.id);
                               }
                          }
                          
                          
                          

                           

                          Now from what I can gather, this code will set each id of "ti" (which is a TextInput) to d01, d02, d03 .... d99.  If I was to do addElement(); in the loop, they would be displayed in the state.

                           

                          I'm just not sure how to do a similar task with existing TextInputs (created in mxml) so I don't have line and lines of code.

                           

                          Thanks

                          Alan

                          • 10. Re: Working with TextInput boxes
                            Devtron Level 3

                            ^ You can loop through your MXML textinput boxes in the same manner, if you name them similarly.