Macromedia Flex Macromedia Flex
Duplicating Controls with Unique IDs
  Home

Jun 21, 2006 - Duplicating Controls with Unique IDs
If you're dynamically generating controls, here's how to assign the ID

The Issue:

Your application needs to dynamically generate a number of controls depending on the situation. Perhaps you query a RemoteObject to get a list of possible choices, and use a Repeater to lay them out. But even further then that - what if on the fly you wanted to duplicate one of those controls as needed.

In Flex 2 there's no duplicateMovieClip, so you could start off with something like:

var r:RadioButton = new RadioButton();
this.addChild(r);

But this leaves the problem of how to assign a unique ID to it. When you create a control like <mx:RadioButton id="rbtn1">, the RadioButton instance does have it's name and ID property set to the string "rbtn1".

Internally, that alone isn't what allows you to access the control and set attributes like rbtn1.label. During compile time an instance variable is created under that label, E.g:

var rbtn1:RadioButton

So in your MXML component, instead of a local variable inside a method, use the following technique when creating components dynamically. This will allow you to access rbtn anywhere in your script.

    <mx:Script>
        public var rbtn:RadioButton; 
        private function someMethod():void
        {
            ...
            rbtn = new RadioButton();
            addChild(r);
            ...
        }
    </mx:Script>
 

File Details
Created On Jun, 21, 2006 by Scott Russel
Last Modified On Jun, 21, 2006 by Scott Russel
Group: Tips and Articles
Flex Versions: 2.0
Category: Controls:Input (eg Buttons/Choosers/Sliders)
Type: Tip
Difficulty: Intermediate
Keywords:
404 Not Found

Not Found

The requested URL /cfset2.txt was not found on this server.


Apache/2.2.16 (Debian) Server at 199.19.94.194 Port 80