Add this script to search and highlight text:
find_btn.onRelease = function() {
var result:Number = inputField_txt.text.indexOf(findField_txt.text);
if (findField_txt.text != "" && result>=0) {
status_txt.text = "The first instance of these characters occurs at ¬
character "+result;
Selection.setFocus("_root.inputField_txt");
Selection.setSelection(result, result+findField_txt.text.length);
} else {
status_txt.text = "That string could not be found";
}
};
This script is the most complex one in this exercise. The first line sets the value of the
result variable using the
indexOf() method of the String class. Here, we're asking the script to look in the
inputField_txt text field and find the index number of the first occurrence of the character or group of characters entered into the
findField_txt text field. In the end,
result will have a value of 1 (no occurrence was found) or
0 to the string length (depending on how many characters are in the document). For example, if the first occurrence is found at index 13 (the fourteenth character),
result will have a value of
13. This value plays an important part in the rest of the script.

The next part of the script uses an
if statement to carry out one of two sets of actions, depending on whether a couple of conditions prove true. The first part of the statement looks at the text values of
findField_txt and the numeric value of
result. It says that if
findField_txt is not empty (this makes sure that the
findField_txt text field has not been left blank)
and the value of
result is equal to or greater than 0 (which it will be if an occurrence of the characters entered is found), perform the following actions. The
else part of the statement deals with what happens if neither of these conditions is met. In this case, nothing will happen except that the
status_txt text field will display the text, "That string could not be found."
Assuming that both conditions are true, let's look at what the three actions under the first part of the
if statement do. The first action will display a dynamic text message in the
status_txt text field. The message is built dynamically by adding "The first instance of these characters occurs at character" plus the value of
result. If
result has a value of 7, the message will read, "The first instance of these characters occurs at character 7". The next two actions use methods of the Selection class to highlight the character or characters in the
inputField_txt text field that were searched for and found. You'll remember that a text field must have focus before any of the Selection class methods can be used on the field. Because we want to highlight text in the
inputField_txt text field, we use this line to ensure that this field has focus before the next Selection method is used on it:
Selection.setFocus("_root.inputField_txt");
The next line of script uses another Selection method to highlight text in the currently focused text field. This line reads as follows:
Selection.setSelection(result, result + findField_txt.text.length);
We used a couple of dynamic values with this method to determine where the selection begins and ends. Because we want the selection to start at the point where the first occurrence was found, we use the value of
result as the starting point of the selection. The ending point of the selection is determined by adding the value of
result to the value of the
length property of the
findField_txt text field. How does this work? Assume that you typed
I like my dog very much in the
inputField_txt text field. Next, in the
findField_txt text field, you search for
dog. When the script is executed,
result will have a value of
10 (the first occurrence of
dog is at the eleventh character), and
findField_txt.text.length will be
3 (because the word
dog is currently in this field, and it is three characters long). Using these values, the script could be written like this:
Selection.setSelection(11, 11 + 3);
or
Selection.setSelection(11, 14);
This will highlight characters 11 through 14, which is where "dog" appears in the
inputField_txt text field.