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>