Table of Contents

TextField.addListener( ) Method Flash 6

registers an object to receive onChanged( ) and onScroller( ) events
theField.addListener(listener)

Arguments

listener

An object with an onChanged( ) and/or onScroller( ) method defined.

Returns

A Boolean indicating whether the listener was added successfully. In practice, addListener( ) always returns true in Flash Player 6. However, in future versions it may return false when not enough memory is available to add the listener.

Description

The addListener( ) method adds listener to the list of objects notified when theField's onScroller( ) or onChanged( ) events fire. The listener argument is an object that defines methods of the name onScroller and onChanged; these are invoked when the corresponding events occur for theField. To stop a listener object from receiving events, use TextField.removeListener( ).

Listener events allow a single TextField to trigger functions on multiple objects. When only one function needs to be invoked in response to a TextField event, a callback handler (which offers simpler syntax) will suffice. Note that a TextField can have callbacks and listeners for the same event. Callback functions are executed before listener methods. When multiple listeners are registered to a TextField, their methods are invoked in the order the listeners were added.

For general information on how listeners and callback functions work, see Chapter 10.

Example

The following code creates a TextField, named demo_txt, and assigns it a listener object, txtListener, that responds to the onChanged( ) event. For comparison, the equivalent callback function approach is also shown. To see the code work, copy it into a new movie, export the movie to Test Movie mode, then enter text in the text field.

// Make the text field.
this.createTextField("demo_txt", 1, 10, 10, 200, 20);
// Display some text in the field.
demo_txt.text = "type text here";
// Make the field user-editable.
demo_txt.type = "input";
// Create an anonymous object to act as the listener.
txtListener = new Object();
// Add the onChanged function to the listener. Notice that the function
// is passed the name of the field that triggered the event.
txtListener.onChanged = function (tfObject) {
  trace(tfObject + " changed. Listener invoked.");
}
// Now add the listener to demo_txt.
demo_txt.addListener(txtListener);
// For comparison, here's how we can do the same thing with a callback function.
demo_txt.onChanged = function () {
  trace(this + " changed. Callback invoked.");
}

See Also

TextField.onChanged( ), TextField.onScroller( ), TextField.removeListener( ); Chapter 10


Table of Contents