Recipe 8.24 Selecting Text with ActionScript

8.24.1 Problem

You want to highlight a portion of the text within a text field.

8.24.2 Solution

Use the Selection.setSelection( ) method.

8.24.3 Discussion

The Selection class has a static method, setSelection( ), which highlights a portion of the text in the text field. The selection is applied to the text field that has focus (see Recipe 8.23). The setSelection( ) method takes two parameters:

startIndex

The beginning, zero-relative index of the text to highlight

endIndex

The index of the character after the text to highlight

For example:

myTextField.text = "this is some text";  // Set the text value. Selection.setFocus("myTextField");       // Bring focus to the text field. Selection.setSelection(0, 4);            // Highlight the word "this".

8.24.4 See Also

Recipe 8.25 and Recipe 8.26



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

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