3 Replies Latest reply on Feb 11, 2009 7:12 AM by Richard_UK

    Referencing Components

      Hi All, hopefully an easy one for you...

      I'm new to Flex and are struggling with component referencing, i have a main.mxml application file that pulls in components to make the final app.

      If i add in a mx:panel component then i can change it's properties within main.mxml using actionscript, however what if i want to change the properties of a child of the panel, for example a label that is made visible using a button from main.mxml?

      Thanks very much.
        • 1. Re: Referencing Components
          Richard_Abbott Level 3
          ChimaeraUK,
          you can use the ID of any element however nested - for example in a current app I have an Accordion containing a Grid containing a GridRow containing a GridItem containing a Text with definition
          <mx:Text id="dateTextInput"/>
          and I can simply say
          dateTextInput.text = "blah"
          without worrying about the nesting.

          To find the immediate children of a visual component you can use
          component.getChildren() which returns an Array, and then iterate around the array eg testing ID, type, value or whatever. For example, elsewhere in the app I add a number of NumericSteppers to a container (depending on some data input), ensure I give a unique ID to each, and then iterate around all the container children which are NumericSteppers and do suitable things with the values.

          Hope that helps,
          Richard

          • 2. Re: Referencing Components
            Level 7

            "ChimaeraUK" <webforumsuser@macromedia.com> wrote in message
            news:gmufuh$1jg$1@forums.macromedia.com...
            > Hi All, hopefully an easy one for you...
            >
            > I'm new to Flex and are struggling with component referencing, i have a
            > main.mxml application file that pulls in components to make the final app.
            >
            > If i add in a mx:panel component then i can change it's properties within
            > main.mxml using actionscript, however what if i want to change the
            > properties
            > of a child of the panel, for example a label that is made visible using a
            > button from main.mxml?

            The best practice way is to create an API on the extended panel and let the
            panel handle it.

            For example, in your main mxml file:

            yourCustomPanel.doStuff('foo');

            In your CustomPanel:

            public function doStuff(stuffToDo:String):void {
            if (yourLabel) yourLabel.text=stuffToDo;
            }


            • 3. Re: Referencing Components
              Level 1
              Thanks all, got it working now.