Skip to content
July 18, 2008 / Shrikant Patil

Passing Additional Parameters To Event Listener

While configuring the event listeners for the a component, some time we want to pass additional parameters to the event listener.

Here is the trick how to do that;

First configure the the listener in this way;


myComponent.addEventListener(MouseEvent.CLICK, function(e:mouseEvent):void{
          var parm1:String = "First Parameter";
          var parm2:String = "Second Parameter";
          myClickHandler(e,parm1,parm2);
});

The write the listener function like this;


function myClickHandler(e,str1,str2):void{
    trace("Parameter 1 :"+str1);
    trace("Parameter 2 :"+str2);
}

Trick Always works well 🙂

Advertisements

14 Comments

Leave a Comment
  1. me / Jul 23 2008 5:36 pm

    thanks!

  2. Shubhra / Aug 26 2008 7:24 am

    Thanks Srikant this problem was causing me a lot of trouble.

  3. nylarch / Feb 28 2009 9:23 pm

    Bravo! This is a tough one to figure when you first want to do it in a project…thanks…

  4. Luise / Dec 28 2009 10:49 pm

    Thanks very much!!!

  5. Sai / Jan 14 2010 3:26 pm

    How do you pass in variables declared outside that function instead of constants ?? Any thoughts ?

  6. Mairum / Feb 12 2010 6:46 pm

    Thanks! Had forgotten how do do it…

  7. lukyluky / Feb 18 2010 4:34 pm

    This example is amazing!
    I’ve already tried to extend the eventclass, to achieve sth like this!
    Didn’t know that it is that easy.
    Tnx a lot!!

  8. Muruganandan Palanisamy / Jun 1 2010 11:37 pm

    great job and you saved me a tons of stress!!!!

  9. vinsu varghese / Jul 20 2010 11:10 am

    Thanks Shrikant,

    That was excellent… really helped me.. keep posting…

  10. Sukash / Jul 23 2010 1:24 pm

    Never think like this..
    Good Job…

  11. Sukash / Jul 26 2010 10:06 am

    Never think, This was that much easy..
    Great Job..

  12. Manohar / Jan 13 2011 1:28 pm

    How to remove the event listener which is assigned like this …!!!!

  13. greg / May 17 2011 9:41 pm

    This code works great. Thanks!

  14. isa / Feb 14 2012 7:25 am

    Thanks for that tip but how dispatch the event and how pass parameter to event dispatcher

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: