3 Replies Latest reply on May 5, 2009 7:47 AM by manofspirit

    AddEventListener and Function with Parameters

    manofspirit Level 1

      Hello guys

       

      I got this situation

       

      nameTxt.addEventListener(FocusEvent.FOCUS_IN,formTextHandler);
      
               private function formTextHandler(text:String):void{
                    
               }
      
      

       

      where i want to send some additional information too

      so how could i do that?

       

      Thanks

        • 1. Re: AddEventListener and Function with Parameters
          ATIF FAROOQ Level 3

          hi,

             it seems like you will need to extend your text Component and Event class as well. Its not clear from your code that what component you are referring to . so generally you will extend your required component .

           

          you will need to addEvent Listener

          FocusEvent.FOCUS_IN

           

          in your custom extended component and in the handler of that focusEvent you will dispatch your new custom event with the parameters from this FocusEvent you received and as well as new parameters that are also required by your new extended Event.

           

          Method of extending events is straight forward. e.g this a sample for extending Events you will need to modify it with the paramaters you require in your event .

           

          package
          {
              import flash.events.Event;
              public class AccountEvent extends Event
              {
                  public var accountObj:Object
                  public static var NewAccount:String='newAccount'
                  public function AccountEvent(type:String,newAccount:Object)
                  {
                      super(type)
                      this.accountObj=newAccount
                  }
          
              }
          }
          
          
          

          and here is the code how to use this event

           

          package 
          {
              import flash.events.Event;
              import flash.events.FocusEvent;
              import AccountEvent
              import mx.controls.TextInput
              public class customText extends TextInput
              {
                  public function customText()
                  {
                      super();
                      addEventListener(FocusEvent.FOCUS_IN,onFocus)
                  }
                  private function onFocus(e:FocusEvent):void{
                      dispatchEvent(new AccountEvent(paramaters.....///here you will add your custom paramerters and  
                     //you will catch this event in your main application rather then catching the focus in event
                  }
                  
              }
          }
          
          • 2. Re: AddEventListener and Function with Parameters
            peteandrus

            the best way to do that is to create a custom Event, like:

             

            class CustomTextHandlerEvent extends Event

            {

             

                CustomTextHandlerEvent(text:String, other params....)

                 {

                      //store your data or process it or whatever...

                 }

             

                 public get text():String

                 {

                      //return the text

                 }

             

            }//end class def

            1 person found this helpful
            • 3. Re: AddEventListener and Function with Parameters
              manofspirit Level 1

              Thanks to you both guys