RichTextBox.Find Method (String, Int32, RichTextBoxFinds)
Searches the text in a RichTextBox control for a string at a specific location within the control and with specific options applied to the search.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
- Type: System.String
The text to locate in the control.
- Type: System.Int32
The location within the control's text at which to begin searching.
Return ValueType: System.Int32
The location within the control where the search text was found.
The Find method searches for the text specified in the str parameter and returns the location of the first character of the search string within the control. If the property returns a negative value, the text string being searched for was not found within the contents of the control. You can use this method to create search functionality that can be provided to the user of the control. You can also use this method to search for text to be replaced with a specific format. For example, if the user entered dates into the control, you could use the Find method to search for all dates in the document and replace them with the appropriate format before using the SaveFile method of the control.
With this version of the Find method, you can specify options that enable you to expand or narrow your search. You can specify options that enable you to match the casing of the search word or to search for entire words instead of partial words. By specifying the RichTextBoxFinds.Reverse enumeration in the options parameter, you can search for text from the bottom of the document to the top instead of the default top to bottom search method. This version of the Find method also enables you to narrow the search for text by selecting a specific starting position within the control's text. This feature can enable you to avoid text that might have already been searched or where the specific text you are searching for is known not to exist. When the RichTextBoxFinds.Reverse value is specified in the options parameter, the value of the start parameter indicates the position where the reverse search will end since the search will start at the bottom of the document when using this version of the Find method.
The following code example searches the entire contents of a RichTextBox for the first instance of a search string passed into the text parameter of the method. The search starting location is specified by the start parameter of the method. If the search string is found in the RichTextBox, the method returns the index location of the first character of the found text and highlights the found text; otherwise, it returns a value of -1. The example also specifies options in the search to match the case of the specified search string. The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1. You can use this example to perform a "Find Next" type of operation. Once an instance of the search text has been found, you can find other instances of the text by changing the value of the start parameter to search at a location beyond the position of the current match.