User Guide
Defining events in ActionScript components 143
You might define some custom events that are used internally by your component, and are
not intended to be recognized by the other components. For example, the following
component defines a custom event, dispatches it, and handles it all within the component:
package myComponents
{
import mx.controls.TextArea;
import flash.events.Event;
public class ModalText extends TextArea {
public function ModalText() {
super();
// Register event listener.
addEventListener("enableChanged", enableChangedListener);
}
public function enableInput(value:Boolean):void {
// Method body.
// Dispatch event.
dispatchEvent(new Event("enableChanged"));
}
private function enableChangedListener(eventObj:Event):void {
// Handle event.
}
}
}
In this example, the public method enableInput() lets the user enable or disable input to
the control. When you call the
enableInput() method, the component uses the
dispatchEvent() method to dispatch the enableChanged event. The dispatchEvent()
method has the following signature:
dispatchEvent(eventObj)
The eventObj argument is the event object that describes the event.
If you want an MXML component to be able to register a listener for the event, you must
make the event known to the Flex compiler by using the
[Event] metadata tag. For each
public event that your custom component dispatches, you add an
[Event] metadata keyword
before the class definition that defines that event; for example:
[Event(name="enableChanged", type="flash.events.Event")]
public class ModalText extends TextArea {
...
}