9 Replies Latest reply on Jun 9, 2010 10:32 PM by subhajitnag1982

    How to set a specific width to all the TextInputs used in a mxml file on its creation complete?

    subhajitnag1982 Level 1

      How to set a specific width to all the TextInputs used in a mxml file on its creation complete?

        • 1. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
          BhaskerChari Level 4

          Hi,

           

          In the creationComplete event of mxml file call a function...and in that set the widths of all the textInpust based on their Id.

           

          creationComplete="onCreationComplete ()" and your onCreationComplete  function should be as follows:

           

          private function onCreationComplete ():void
            {
             textInput1.width = 250;
             textInput2.width = 200;
             textInput3.width = 300;
            }

           

          In above textInput1, textInput2 and textInput3 are the ids of your TextInputs.

           

          If this post answers your question or helps, please kindly mark it as such.


          Thanks,

          Bhasker Chari

           

          • 2. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
            subhajitnag1982 Level 1

            This is a manual process of calling each and every textinputs' id and setting that to a specific width.

            Even i can store all in an array and set that to specific width.

            But i dnt want that, i want that on creation complete the method will run a check that how many textinputs are present in that current mxml and will set the width on those. So that later on adding any new textinputs wont need me to update the method for that

            • 3. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
              David_F57 Level 5

              hi,

               

              You can try this code...

               

              David

               

               

              <?xml version="1.0" encoding="utf-8"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                 xmlns:s="library://ns.adobe.com/flex/spark"

                 xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

                 creationComplete="application1_creationCompleteHandler(event)">

              <fx:Script>

              <![CDATA[

              import mx.events.FlexEvent;

               

              protected function application1_creationCompleteHandler(event:FlexEvent):void

              {

              var obj:Object;

              for (var i:Number=0;i < this.numElements;i++)

              {

              obj = this.getElementAt(i);

              if (flash.utils.getQualifiedClassName(obj) == "spark.components::TextInput")

              {

              (obj as TextInput).width=150;

              }

              }

              }

              ]]>

              </fx:Script>

              <fx:Declarations>

              <!-- Place non-visual elements (e.g., services, value objects) here -->

              </fx:Declarations>

              <s:TextInput x="180" y="191" width="102"/>

              <s:TextInput x="180" y="354" width="197"/>

              <s:TextInput x="180" y="297" width="68"/>

              <s:TextInput x="180" y="242" width="164"/>

              <s:TextInput x="176" y="134" width="241"/>

              </s:Application>

              1 person found this helpful
              • 4. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                BhaskerChari Level 4

                Hi David,

                 

                I dont think the code you gave will work in all instances ....

                 

                It is because for your code to work all the textinput should be placed in the main application with out being enclosed by containers.

                 

                The same way you have placed in the sample example you have posted. But if you place the TextInputs in any containers then your approach defenitely fails. Since for loop only loops through the subchilds of the main application but not the subchilds of the subchilds of main application.

                 

                I hope you would agree with this...

                 

                Thanks,

                Bhasker Chari

                • 5. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                  David_F57 Level 5

                  hi,

                   

                  Of course the code won't work in all instances, its an example of how to iterate and set parameters, for more complex iteration I would expect that the example suffices enough for the poster to continue with a more complex iteration that will work through any containers in the application. There are several approaches that can be used my example is just one.

                   

                  I come into the forums to assist poster's, not be criticized on what is a functional example of one way to approach the altering  of components without having to refer to their id's individually.

                   

                  Also without the need of complex iteration the function could just be called in the show event of any container and that way the time to achieve the desired result would be far quicker than iterating through a whole application.

                   

                  David.

                  • 6. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                    subhajitnag1982 Level 1

                    Hey David

                     

                    Thanx for the idea man but what Bhasker has said is absolutely correct. this idea is definitely workin if the textinput resides directly on the mxml and nt within any container.

                     

                    My requirement is that the application which i have built has many textinputs in various mxml files. I have been asked to change the textinputs' dimension to a certain value and there is a possibility that on later stage i might have to change again. So for ths scenario i want a method to fire for every mxml's creation complete which will dynamically count all the textinputs within it and within its sub and sub-sub containers and set the specified dimension.

                    • 7. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                      BhaskerChari Level 4

                      Hi subhajit nag,

                       

                      I think the best approach would what I say is if you can make use of a Global variable or using a Singleton class having a variable which can be accessed globally through out the application.

                       

                      Below is the sample what I am talking to:

                       

                      So in below example you can access the variables textInputWidth and textInputHeight globally throghout the application since you have decalred them in main mxml file.

                       

                      You can access these variabled in another mxml file as Application.application.textInputWidth and Application.application.textInputHeight. So if at all you are required to modify these values you are only required to modify at a single place in main mxml file..thats it.

                       

                      If you are use any Singleton classes like ModelLocator you can place these variables in this class and refer from that class.

                       

                      Hope this resoved your problem..

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="
                      http://www.adobe.com/2006/mxml" layout="vertical">
                      <mx:Script>
                        <![CDATA[
                         [Bindable]private var textInputWidth:int = 250;
                         [Bindable]private var textInputHeight:int = 25;
                        ]]>
                      </mx:Script>

                      <mx:Canvas id="canvas1">
                        <mx:TextInput id="txt1" width="{textInputWidth}" height="{textInputHeight}"/> 
                      </mx:Canvas>
                      <mx:Canvas id="canvas2">
                        <mx:TextInput id="txt2" width="{textInputWidth}" height="{textInputHeight}"/> 
                      </mx:Canvas>
                      <mx:Canvas id="canvas3">
                        <mx:TextInput id="txt3" width="{textInputWidth}" height="{textInputHeight}"/> 
                      </mx:Canvas>
                      <mx:TextInput id="txt4" width="{textInputWidth}" height="{textInputHeight}"/>
                      <mx:TextInput id="txt5" width="{textInputWidth}" height="{textInputHeight}"/>
                      </mx:Application>

                      If this post answers your question or helps, please kindly mark it as such.


                      Thanks,

                      Bhasker Chari

                       

                      1 person found this helpful
                      • 8. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                        BhaskerChari Level 4

                        Hi subhajit nag,

                         

                        Have you tried this approach...?

                         

                        Thanks,

                        Chari

                        • 9. Re: How to set a specific width to all the TextInputs used in a mxml file on its creation complete?
                          subhajitnag1982 Level 1

                          Yes i already tried and in ths way it works and also have one more idea in my mind

                          I can create a custom textinput box by extendin it and setting a specific width there and in place of general textinput will call that custom textinput.

                           

                          but i am actually expecting of somethng more dynamic like wat is my requirement.

                          without giving any bindable variables nor makin any custom txtinput