Manipulating Text with the Selection Object Flash

The last text feature that we discuss is the new Flash 5 Selection Object. The Selection Object is similar to the Mouse Object you don’t create instances of the Selection Object, as there can only be one active text field at any given time. In Flash 4 movies, we had no way of checking which text field was active. You could turn off a focus rectangle for Flash 4 text fields and Button instances, but you couldn’t control tab order or automatically set a text field active.

Either Input or Dynamic Text fields can use the Selection Object. The Selection Object uses a string reference to the text field’s variable name to perform its methods. We discuss the methods of the Selection Object in the following sections.

getBeginIndex
This method detects and returns the starting position of a highlighted selection in a text field. The method returns –1 if there is no active text field and/or there is no selection inside the text field. As with the Array Object, selection indexes start position values at 0. You do not need to specify a target path for this method only one text field can have a selection at any given point. Therefore, as a variable startIndex, the getBeginIndex() method would look like:

In the Output window, the trace action would reveal startIndex = -1 until you made a selection within a text field in the movie, as shown below.

A text field with a starting selection index of 3

A text field with a starting selection index of 3

getEndIndex
Similar to the getBeginIndex() method, this method returns a number indicating the index position at the end of a highlighted selection in a text field, as shown below. If there is no active selection, then a value of –1 will be returned.

A text field with a starting selection index of 5 and an ending index of 9

A text field with a starting selection index of 5 and an ending index of 9

getCaretIndex
This method of the Selection Object returns the current cursor position (as an index value) within an active text field, as shown below. As with the two previous methods, if you use the getCaretIndex() method when there is no active cursor in a text field, it will return a –1.

A text field with a caret index of 5

A text field with a caret index of 5

getFocus
This method returns the current active text field’s name, as an absolute path; that is, if you have selected or inserted the cursor position inside a text field named myOutput on the Main Timeline, then Selection.getFocus()returns _level0.myOutput. If there is no active text field, then this method returns null.

setFocus
Perhaps the best enhancement to controlled text field activity is the setFocus() method. This method enables you to make a text field active automatically the user doesn’t need to click the mouse cursor inside the text field to start typing. To use this method, simply indicate the setFocus() method of the Selection Object, and the absolute path to the text field as its argument:

This code, when used on an empty Movie Clip instance, sets the current focus to the testOutput text field. If any text exists in the text field, it will be highlighted as a selection. You can only use string data types as the setFocus() argument. If you try the following:

The setFocus() method will not work. Why? The first line of code sets myTextField equal to the value of _root.testOutput, not to the text field’s name itself. To remedy the situation, simply refer to the text field’s name in quotes:

setSelection
The last method available for the Selection Object is setSelection(). This method enables you make a specific selection within an active text field. The method takes two arguments: a start index and an end index. Using the same index numbering as getBeginIndex() and getEndIndex(). Note that this method will not work unless a text field is already active. The following code creates a selection span from index 5 to 9 of the testInput text field:


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics