public static setSelection(beginIndex:Number, endIndex:Number) : Void
Sets the selection span of the currently focused text field. The new selection span begins at the index specified in the beginIndex parameter, and ends at the index specified in the endIndex parameter. Selection span indexes are zero-based (for example, the first position is 0, the second position is 1, and so on). This method has no effect if no text field currently has focus. When you call the setSelection() method and a text control has focus, the selection highlight is drawn only when the text field is being actively edited. The setSelection() method can be invoked after Selection.setFocus() or from within an onSetFocus() event handler, but any selection is visible only following a call to the fscommand activateTextField command.
beginIndex:Number - The beginning index of the selection span.
endIndex:Number - The ending index of the selection span.
The following ActionScript code creates a text field at runtime and adds a string to it. Then it assigns an event handler for the onSetFocus event that selects all the text in the text field and activates the editing session.
Note: If the Selection.setSelection() method is called, the text is not drawn on screen until the text field is activated (following a call to the fscommand activateTextField command).
this.createTextField("myText_txt", 99, 10, 10, 200, 30);
myText_txt.type = "input";
myText_txt.text = "this is my text";
myText_txt.onSetFocus = function(){
Selection.setSelection(0,myText_txt.text.length);
fscommand("activateTextField");
}
The following example illustrates how the endIndex parameter is not inclusive. In order to select the first character, you must use an endIndex of 1, not 0. If you change the endIndex parameter to 0, nothing will be selected.
this.createTextField("myText_txt", 99, 10, 10, 200, 30);
myText_txt.text = "this is my text";
this.onEnterFrame = function () {
Selection.setFocus("myText_txt");
Selection.setSelection(0, 1);
delete this.onEnterFrame;
}