TextField.onChanged( ) Event Handler and Listener Event

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
TextField.onChanged( ) Event Handler and Listener Event Flash 6

invoked when user input is detected
theField.onChanged(changedField) listener.onChanged(changedField)

Arguments

changedField

The TextField object for which input was detected.

Description

The onChanged( ) event is triggered whenever the user enters or deletes text in theField. Typically, onChanged( ) is used to trigger input-validation routines in response to the user's entries while the user is still typing. Possible uses include an inline spellchecker, an email validator, or an autocomplete feature for a text field.

Note that onChanged( ) is not triggered when new text is assigned to theField through ActionScript. For example, this statement does not trigger onChanged( ):

theField_txt.text = "Some new text...";

If theField.restrict is true and the user attempts to enter a restricted character, then the keystroke is ignored and onChanged( ) is not triggered.

The onChanged( ) event can be handled with a callback function attached to the text field. The following code registers a callback function that responds to onChanged( ):

// Create a text field that accepts user input this.createTextField("theField_txt", 1, 0, 0, 200, 20); theField_txt.type = "input"; theField_txt.border = true; // Assign the onChanged() callback using a function literal theField_txt.onChanged = function (changedField) {   trace("The user typed something in " + this); };

Notice that from the body of the callback we refer to theField_txt using the this keyword; we could also have used the changedField parameter to obtain a reference to the text field that changed.

The onChanged( ) event can also be handled by an event listener that has been added using addListener( ), as specified by listener. When using an event listener, we must use the changedField parameter to determine which text field has changed. The following code registers an event listener that responds to onChanged( ):

changedListener = new Object(); changedListener.onChanged = function (changedField) {   trace("The user typed something in " + changedField); }; theField_txt.addListener(changedListener);

Using a callback function is simpler, but it allows only a single function to respond to the event. Any number of listener objects can respond to a single event. To stop a callback from handling the onChanged( ) event, use the delete operator, as in:

// Make sure to omit the () operator after the function name! delete theField_txt.onChanged;

To stop a listener from handling the onChanged( ) event, use removeListener( ), as in:

theField_txt.removeListener(changedListener);

Bugs

When text is entered rapidly into a text field (for example, when many keys are pressed simultaneously), the new value of theField.text may not yet be set when the onChanged( ) handler executes. When rapid typing is expected, use setInterval( ) to wait until the new text is available.

See Also

TextField.addListener( ), TextField.removeListener( ), TextField.type; Chapter 10.



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net