2 Replies Latest reply on Jul 21, 2009 12:47 PM by Louder6Pounder

    Custom events

    Louder6Pounder Level 1

      Hi,

       

      I would like to fire an event from one class and listen to it from another.  There are several examples on the net but i don't really understand them.  Can someone please explain how i can get the value of newTextInput in the class below on change and pick it up in another class. 

       

                public function changeHandler(event:Event):void{
                     
                }
                
                //  Creates a text input 
                public function setText(label:String, type:String, id:String):void{
                     var newFormItem:FormItem = new FormItem;
                     var newTextInput:TextInput = new TextInput;
                                    
                     newFormItem.label = label;
                     newTextInput.id = type;
                     newTextInput.text = id;
                     newTextInput.addEventListener(Event.CHANGE, changeHandler);
                                    
                     this.addChild(newFormItem);
                     newFormItem.addChild(newTextInput);                                   
                }
      

       

      This is the class i want to get the value of newTextInput:

       

      package components{
           
           import mx.controls.TextArea;
                
           public class KamTextArea extends TextArea{
                
                public var label:String;
                public var required:Boolean;
                
                public function KamTextArea(){
                     label = "Null";
                     required = false;           
                                    
                }
      
                public function setupProperties(propertyInstance:PropertiesForm):void{
                     
                     propertyInstance.setText('Label','label',this.id);
                     propertyInstance.setBool('Required', 'required');
                
                }
                
                private function update():void{
                     this.text = "df";
                     
                }
      
           }
      }
      

       

       

      Thanks

        • 1. Re: Custom events
          Gregory Lafrance Level 6

          You need to understand the basics of custom events, so please read this Adobe LiveDocs page:

           

          http://www.google.com/search?hl=en&q=flex+3+dispatching+custom+events&aq=1&oq=flex+3+dispa tching&aqi=g2

           

          So create a custom event, and make the event type "myEvent".

           

          Then have one property of the custom event be the value of newTextInput.

           

          Your other class listens for the event, and accesses the property of the event object that contains the value ofnewTextInput.

           

          You may need to add the event listener to systemManager:

           

          systemManager.addEventListener(MyCustomEvent.MY_EVENT, myHandler);

           

          My Flex Cookboo post deals with custom events. It's a bit long, but if you get it working locally it may clear up some points.

           

          http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postI d=11246

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: Custom events
            Louder6Pounder Level 1

            Cheers again Greg.  I actually managed to work it out myself.  It wasn't working because i had the listener in the wrong place.  For anyone who finds this this is how i did it. 

             

                 public function changeHandler(event:Event):void{
                           dispatchEvent(new Event("fieldChange"));
                           currentField = event.currentTarget.text;     
                      }
                      
                      //  Creates a text input 
                      public function setText(label:String, type:String):void{
                           var newFormItem:FormItem = new FormItem();
                           var newTextInput:TextInput = new TextInput();
                                          
                           newFormItem.label = label;
                           newTextInput.id = type;
                           newTextInput.text = id;
                           newTextInput.addEventListener(Event.CHANGE, changeHandler);     
                                                    
                           this.addChild(newFormItem);
                           newFormItem.addChild(newTextInput);
                      }
            

            above is in the class where i want to value to come from

             

            public function setupProperties(propertyInstance:PropertiesForm):void{
                           propertyDescriptionInstance = propertyInstance;
                           propertyDescriptionInstance.addEventListener("fieldChange", newFormItemAddedHandler);
            
                           propertyDescriptionInstance.setText('Label','label');
                           propertyDescriptionInstance.setBool('Required', 'required');
                                          
                      }
            

             

            above is the class where i wanted to recieve the value