Visual Basic: RichTextBox Control

Find Method

See Also    Example    Applies To

Searches the text in a RichTextBox control for a given string.

Syntax

object.Find(string, start, end, options)

The Find method syntax has these parts:

Part Description
object Required. An object expression that evaluates to an object in the Applies To list.
string Required. A string expression you want to find in the control.
start Optional. An integer character index that determines where to begin the search. Each character in the control has an integer index that uniquely identifies it. The first character of text in the control has an index of 0.
end Optional. An integer character index that determines where to end the search.
options Optional. One or more constants used to specify optional features, as described in Settings.

Settings

The setting for options can include:

Constant Value Description
rtfWholeWord 2 Determines if a match is based on a whole word or a fragment of a word.
rtfMatchCase 4 Determines if a match is based on the case of the specified string as well as the text of the string.
rtfNoHighlight 8 Determines if a match appears highlighted in the RichTextBox control.

You can combine multiple options by using the Or operator.

Remarks

If the text searched for is found, the Find method highlights the specified text and returns the index of the first character highlighted. If the specified text is not found, the Find method returns 1.

If you use the Find method without the rtfNoHighlight option while the HideSelection property is True and the RichTextBox control does not have the focus, the control still highlights the found text. Subsequent uses of the Find method will search only for the highlighted text until the insertion point moves.

The search behavior of the Find method varies based on the combination of values specified for the start and end arguments. This table describes the possible behaviors:

Start End Search Behavior
Specified Specified Searches from the specified start location to the specified end location.
Specified Omitted Searches from the specified start location to the end of the text in the control.
Omitted Specified Searches from the current insertion point to the specified end location.
Omitted Omitted Searches the current selection if text is selected or the entire contents of the control if no text is selected.