4 Replies Latest reply on Mar 14, 2010 9:28 PM by rasmikant

    Accessing a dynamically  created control

    rasmikant

      I have  created a method inside a class to create a combobox. here is my code

       

      package com.inf
      {
      import mx.controls.*;
      public class ClsInf
      {
        public function ClsInf()
        {

        }
         
      public function addDistDrp(source):void{

      var objDist:ComboBox=new ComboBox();
        objDist.id="cmbDist";
        source.addChild(objDist);
      }

       

      }

       

      }

       


      in my AS file  I wrote

       

      var objInf:ClsInf=new ClsInf();

      objInf.addDistDrp(myTargetId);

       

       

      here I am able to create the combobox. But Now My problem is here how access the combobox so that I can fill data, disable it, set style inti it etc.

      suppose if I have to set a style into it . I am trying to access it by its ID like

       

      cmbDist.styleName="clearButton2"

       

      which showing error. how can I access . Pls Help!

       

       

       

        • 1. Re: Accessing a dynamically  created control
          AdrianWiecek Level 3

          Hi,

           

          I'm trying to figure out what you are trying to achieve. Do you want to create composite Flex component having ComboBox inside? Any way, you can create property and public getter to return current ComboBox instance, or alternatively your method can return this newly created ComboBox instance and use it later in your program:

           

           

          public function addDistDrp(source):ComboBox

          {

               var objDist:ComboBox=new ComboBox();
               objDist.id="cmbDist";
               source.addChild(objDist);

               return objDist;
          }

           

           

          var objInf:ClsInf=new ClsInf();

           

          var comboBox:ComboBox = objInf.addDistDrp(myTargetId);

           

           

           

          Regards,

          Adrian Wiecek

          1 person found this helpful
          • 2. Re: Accessing a dynamically  created control
            rasmikant Level 1

            Thanks Adrian, This is working if I am calling the addDistDrop method directly But here is a twist . Inside that class I have written one more method ie

             

            public function createNavBar(source,type):void{
               switch(type)
               {
                case "Dist":
                 addStateBtn(source);
                 addDistDrp(source);
                 break;


                case "Block":
                 addStateBtn(source);
                 addDistDrp(source);
                 addBlockDrp(source);
                 break;   


                default:
                addStateBtn(source);
               }
             
            }

             

             

            and inside actionscript I have written

             

            var objInf:ClsInf=new ClsInf()
            var strType:String="Block";   
            objInf.createNavBar(myTargetId,strType);

             

             

            so here how can I get the cmbDist combobox

            • 3. Re: Accessing a dynamically  created control
              AdrianWiecek Level 3

              Hi,

               

              So you're creating multiple components in your class, but do you add them to stage? Is your class also a component which is added to stage or it's only a builder/creator of components? However if your class inherits directly or indirectly from  EventDispatcher, you can create your own Event which describes what has been created, and holds a reference to newly created UIComponent:

               

               

              public class MyEventClass extends Event

              {

                   public static const COMPONENT_CREATED:String = "componentCreated";

               

                   public var component:UIComponent;

               

               

                   public function DiagrammaEvent(type:String, component:UIComponent, bubbles:Boolean=true, cancelable:Boolean=false)

                   {

                        super(type, bubbles, cancelable);

                        this.component = component;

                   }

               

               

                   override public function clone():Event

                   {

                        return new DiagrammaEvent(type, component, bubbles, cancelable);

                   }

              }

               

               

              In your addButton or addComboBox methods, you would dispatch this event, and pass instance of newly created component. In your app, you would listen for this event, and manipulate with it. Here's some pseudo code:

               

              public function addComboBox():void

              {

                   var  cb:ComboBox = new ComboBox();

                   dispatchEvent(new MyEventClass(MyEventClass.COMPONENT_CREATED, cb));

              }

               

              protected function componentCreatedHandler(event:MyEventClass):void

              {

                   var component:UIComponent = event.component;

                   if(component is ComboBox)

                   {

                        //newly created component is ComboBox,

                   }

                   if(component is Button)

                   {

                        //newly created component is Button,

                   }

                   //and so on...

              }

               

               

              Regards,

              Adrian Wiecek

              • 4. Re: Accessing a dynamically  created control
                rasmikant Level 1

                hy adrian, Thanks for your reply.

                 

                But I am not getting what you are trying to say. Actually my class is creating a component with multiple comboboxes and also adding into the stage. So how can I get each combobox to take a action agnaist in it.