Adding an event listener if one isn't present : List Event « Components « Flex






Adding an event listener if one isn't present

Adding an event listener if one isn't present
           
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
    height="100%" layout="vertical">
    <mx:Script>
        
        import mx.controls.Alert;
        [Bindable]
        public var labelText:String = "Label before event";
        
        public function clickFunction(event:Event):void
        {
            var message:String = "you clicked me! " + event.target.label;
            Alert.show(message,"Event Test");
        }
        public function addMyEvent():void
        {
            if (myButton.hasEventListener(MouseEvent.MOUSE_OVER)) {
                myButton.removeEventListener(MouseEvent.MOUSE_OVER,changeLabel);
                myInput.text = "Event was Removed";
                addEventButton.label ="Click this button to add event";
                myLabel.text = "Mouse over Event listener removed";
            } else {
                myButton.addEventListener(MouseEvent.MOUSE_OVER,changeLabel);
                myInput.text = "Event was added";
                addEventButton.label ="Click this button to remove event";
            }
        }
        public function changeLabel(event:MouseEvent):void
        {
            myLabel.text = "Mouse over";
        }
      
    </mx:Script>
    <mx:Button id="addEventButton" label="click this button to add listener" click="addMyEvent()"/>
    <mx:Button id="myButton" label="Click button!" click="clickFunction(event)"/>
    <mx:Label id="myInput"/>
    <mx:Label id="myLabel" text="{labelText}"/>
</mx:Application>

   
    
    
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Change event for List controlChange event for List control
2.Event listener in an ActionScript function in an tagEvent listener in an ActionScript function in an <mx:Script> tag
3.List change eventList change event
4.Get selected item from a list from change eventGet selected item from a list from change event
5.Convert event target to List controlConvert event target to List control
6.Write your event listener to access the event objectWrite your event listener to access the event object
7.A data-binding watcher invokes an event listener when a binding occurs.A data-binding watcher invokes an event listener when a binding occurs.
8.List dragOver, dragDrop, dragExit eventsList dragOver, dragDrop, dragExit events
9.List double click eventList double click event
10.Add an Event Listener in MXMLAdd an Event Listener in MXML