How to pass additional parameters to an event listener in Flex

In this example, we will look at how we can pass additional parameters to an event listener using Flex’s dynamic function construction. In the example below we call the addArguments() method with the parameter we want to pass when creating the event listener for the Alert window.

The addArguments() method will then return the function the event listener will call when the event is triggered complete with our extra parameters.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx=""
width="400" height="300">


import mx.controls.Alert;

// This method will return our function with the additional parameters included
private function addArguments(method:Function, additionalArguments:Array):Function
  return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}

// Handle the Alert box closing
private function alertCloseHandler(e:CloseEvent, theText:String):void
  if (e.detail == 1) // Yes
    lblResult.text = theText.split("").reverse().join("");
  else // No
     lblResult.text = theText;

// When setting up the closeHandler for the Alert window we call the addArguments()
// method and pass in an array continaing all of the parameters we want to pass to
// the closeHandler. The addHandler() method will return the function we will call
// when the Alert window closes with the parameters included.
protected function btnOK_clickHandler(event:MouseEvent):void
{"Would you like to reverse the text you just entered?", "", Alert.YES | Alert.NO, null, addArguments(alertCloseHandler, [txtInput.text]), null, Alert.YES);
  txtInput.text = "";


  <s:TextInput id="txtInput"  x="10" y="26" maxChars="50"/>
  <s:Label x="10" y="10" text="Please enter some text and click OK"/>
  <s:Label x="10" y="75" text="Result:" fontSize="14" fontWeight="bold"/>
  <s:Label x="10" y="97" id="lblResult"/>
  <s:Button x="146" y="27" label="OK" id="btnOK" click="btnOK_clickHandler(event)"/>


Get Adobe Flash player

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>