Hi Keith,
Main issue is that there is nothing to distinguish the TextInput instances.
Plus the I iterative isn't working because it indeed will be that last number
of the loop it's not being used for anything else or assigned to anything.
To get around this, you could assign a different name to each of the input
boxes in the loop. So using your I iterative you could have: input.name =
"inputbox" + String(i);
Then in your event listener have:
Trace("input box changed = " + Event.currentTarget.name);
That should work ;)
Hope that helps.
Dave.
--- In [email protected], "Keith Hughitt" <keith.hugh...@...> wrote:
>
> Could someone please explain to me how closures work in ActionScript? I
> am attempting to create a set of Flex Form fields, and assign an
> event-handler to each of them, however, it seems that after creation,
> all of the fields have the same event handler.
>
> For example:
>
> var i:int = 0;
> for each (var item:Object in this._myItems) {
> var f:FormItem = new FormItem();
> f.label = item.header;
>
> var input:TextInput = new TextInput();
>
> // Setup event-handler
> var self:MyClass = this;
>
> input.addEventListener("change", function (e:Event):void
> {
> Alert.show("Event-hanlder #: " + String(i));
> });
>
> i++;
>
> // Add text input to FormItem
> f.addChild(input);
>
> // Save a reference to the form control
> item.ui = f;
> addChild(f);
> }
>
> So I would expect that the number displayed when I change any given
> field matched the order of the field on the screen. When I run the code,
> however, no matter which field I adjust, the number is always the same
> (it is the number of the last form item to be added).
>
> It appears that what is happening is that a single function is being
> created, and each input field has a reference to that same function.
>
> Any suggestions? Any help would be greatly appreciated.
>
> Thanks!
> Keith
>