6 Replies Latest reply on Sep 21, 2010 3:37 AM by athiroy

    Display Labels using Arrays

    athiroy Level 1

      hi

       

      i am working in flex builder 4.
      I give input (for example 10) in text box .When i click the button at the time it produces 10 Labels.

       

      how i do it
      Give me some suggestion
      regards
      athi

        • 1. Re: Display Labels using Arrays
          BhaskerChari Level 4

          Hi athiroy,

           

          You can do something like below...the example is in Flex3 however you can apply the same logic in Flex4 just the syntax will chnage..instead of addChild you use addElement..

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center">
          <mx:Script>
            <![CDATA[
             import mx.controls.Label;
                                                             
                private function onButtonClick():void
                {
                 var inputValue:Number = Number(textInput.text);
                 for(var intLoop:int=0;intLoop<inputValue;intLoop++)
                 {
                  var lbl:Label = new Label();
                  lbl.text = "Label" + (intLoop + 1);
                  labelBox.addChild(lbl);
                 }
                }
            ]]>
          </mx:Script>
          <mx:HBox>
            <mx:TextInput id="textInput" width="200" height="25" restrict="0-9"/>
            <mx:Button id="btn" label="Click Me" click="onButtonClick();" />
          </mx:HBox>
          <mx:VBox id="labelBox">
           
          </mx:VBox>
          </mx:Application>

           

          Thanks,

          Bhasker

          • 2. Re: Display Labels using Arrays
            athiroy Level 1

            hi

            thank u very much.

            Its working fine.

            regards

            athi

            • 3. Re: Display Labels using Arrays
              athiroy Level 1

              hi

              i use ur program. Its working fine.

              Now i want write Click method in that label.

              When i click that label it must displays the label.text.

              how i do it.

              pls give some ideas.

              regards

              athi

               

              Message was edited by: athiroy

              • 4. Re: Display Labels using Arrays
                athiroy Level 1

                hi

                how can i write the click method in this label.

                 

                regards

                athi

                • 5. Re: Display Labels using Arrays
                  BhaskerChari Level 4

                  Hi Athiroy,

                   

                  You can write the click method as shown in the below example...

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center">
                  <mx:Script>
                    <![CDATA[
                     import mx.controls.Alert;
                     import mx.controls.Label;
                                                                     
                        private function onButtonClick():void
                        {
                         var inputValue:Number = Number(textInput.text);
                         for(var intLoop:int=0;intLoop<inputValue;intLoop++)
                         {
                          var lbl:Label = new Label();
                          lbl.text = "Label" + (intLoop + 1);
                          lbl.addEventListener(MouseEvent.CLICK, onLabelClick);
                          labelBox.addChild(lbl);
                         }
                        }
                       
                        private function onLabelClick(event:MouseEvent):void
                        {
                         var label:Label = event.currentTarget as Label;
                         Alert.show("You have clicked the label : " + label.text);
                        }
                    ]]>
                  </mx:Script>
                  <mx:HBox>
                    <mx:TextInput id="textInput" width="200" height="25" restrict="0-9"/>
                    <mx:Button id="btnFilter" label="Generate Labels" click="onButtonClick();" />
                  </mx:HBox>
                  <mx:VBox id="labelBox">
                   
                  </mx:VBox>
                  </mx:Application>

                   

                   

                  Thanks,

                  Bhasker

                  1 person found this helpful
                  • 6. Re: Display Labels using Arrays
                    athiroy Level 1

                    hi

                    thanks a lot.

                     

                    regards

                    athi