You can use the data property, which is normally used in combination with item 
renderers or item editors, but nothing says you can't 
use it for something else ;-)


   import mx.controls.Button;

   private function appInit():void {
    var offset:Number = 0;
    for (var i:Number=0; i<10; i++) {
     var b:Button = new Button();
     b.data = {btnIndex:i};
     b.y = offset;
     b.label = "click me";
     b.toolTip = "button "+i;
     b.addEventListener(MouseEvent.CLICK, buttonClickHandler);
     addChild(b);
     offset+=20;
    }
   }

   private function buttonClickHandler(evt:Event):void {
    trace("index: "+evt.currentTarget.data.btnIndex);
   }

regards,
Muzak

----- Original Message ----- 
From: "dougco2000" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Friday, March 30, 2007 8:43 AM
Subject: [flexcoders] passing data to an EventListener


I'm creating a number of buttons programmatically and I'd like to have
a unique value passed along to a function when each one is clicked.

So if I am doing something like:

for (i=0;i<10;i++) {
  b = new Button();
  b.y = offset;
  b.label = "click me";
  b.tooltip = "button "+i;
  b.addEventListener(MouseEvent.CLICK, butSelect);
  addChild(b);
  offset+=20;
}

And I want to have, let's say, "i" passed along to the "butSelect"
function, how is that done? I humored myself and tried doing
butSelect(i) but of course the mouseEvent is passed and you're not
allowed to provide any parameters to the function.

Thanks!



Reply via email to