Sam
Sam

Reputation: 11

load text input control in Adobe flex dynamically?

<fx:Script>
        <![CDATA[
            import mx.controls.*;
            import mx.controls.TextInput;
            import mx.events.ListEvent;

            public function init():void
            {
                for(var i:int=0;i<5;i++)
                {       
                    var txtbox:TextInput = new TextInput();     
                    txtbox.id   = "text"+i;

                    myHBox.addChild(txtbox);
                }
            }

            public function getVal():void
            {

            }
        ]]>
    </fx:Script>

    <mx:HBox id="myHBox" width="100%" height="100%">
        <mx:Button label="Get Value" click="getVal()"/>
    </mx:HBox>

I have implemented this one. I am getting 5 textboxes with empty value, if i entered some value in each textbox then, i want to get specific 3rd textbox value wen some event trigger. so how i can i make. since am new to flex. Pls give me solutions. Thanks in advance.

Upvotes: 1

Views: 652

Answers (2)

J_A_X
J_A_X

Reputation: 12847

Why don't you just store the values in its own data structure?

<fx:Script>
        <![CDATA[
            import mx.controls.*;
            import mx.controls.TextInput;
            import mx.events.ListEvent;

            private var inputs:Vector.<TextInput> = new Vector.<TextInput>();

            public function init():void
            {
                for(var i:uint = 0; i<5; i++)
                {       
                    var txtbox:TextInput = new TextInput();  
                    inputs.push(txtbox);  
                    myHBox.addChild(txtbox);
                }
            }

            public function getVal():void
            {
                var value:String;
                for(var i:uint = 0, len:uint = inputs.length; i<len; i++)
                {       
                    value += inputs[i].text + ' ';
                }
                trace(value);
            }
        ]]>
    </fx:Script>

    <mx:HBox id="myHBox" width="100%" height="100%">
        <mx:Button label="Get Value" click="getVal()"/>
    </mx:HBox>

Also, if this is a new project, why are you using Flex 3?

Upvotes: 1

Dennis Jaamann
Dennis Jaamann

Reputation: 3565

Your question is not all too clear, but if I understand correctly,

Try this:

        public function init():void
        {
            for(var i:int=0;i<5;i++)
            {       
                var txtbox:TextInput = new TextInput();
                //txtbox.id   = "text"+i;
                txtbox.name   = "text"+i;
                txtbox.addEventListener(Event.CHANGE,onChange);

                myHBox.addChild(txtbox);
            }
        }

        private function onChange(event:Event):void{
                Alert.show(TextInput(event.target).text,TextInput(event.target).name + " Changed");
        }

        public function getVal():void
        {
            Alert.show(TextInput(myHBox.getChildByName("text3")).text,"Value");
        }

cheers

Upvotes: 0

Related Questions