User Guide

Working with events 111
The following main application includes TextAreaEnabled.mxml and defines an event listener
for the
enableChanged event:
<?xml version="1.0"?>
<!-- mxmlAdvanced/MainTextAreaEnable.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<mx:Script>
<![CDATA[
import flash.events.Event;
import myComponents.TextAreaEnabled;
public function handleEnableChangeEvent(eventObj:Event):void {
var tempTA:TextAreaEnabled =
eventObj.currentTarget as TextAreaEnabled;
if (tempTA.enableTA) {
myButton.label="Click to disable";
}
else {
myButton.label="Click to enable";
}
}
]]>
</mx:Script>
<MyComp:TextAreaEnabled id="myTA" enableTA="false"
enableChanged="handleEnableChangeEvent(event);" />
<mx:Button id="myButton" label="Click to enable"
click="myTA.enableTA=!myTA.enableTA;" />
</mx:Application>
If you do not use the [Event] metadata tag in the custom component file to define the
enableChanged event, the MXML compiler generates an error message when you reference
the event name in an MXML file. Any component can register an event listener for the event
in ActionScript using the
addEventListener() method, even if you omit the [Event]
metadata tag.
You can also create and dispatch events that use an event object of a type other than that
defined by the Event class. For example, you might want to create an event object that
contains new properties so that you can pass those properties back to the referencing file. To
do so, you create a subclass of the Event class to define your new event object. For information
on creating custom event classes, see Chapter 4, “Creating Custom Events,” on page 35.