RichTextBox.Find Method (String)


Searches the text in a RichTextBox control for a string.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int Find(
	string str


Type: System.String

The text to locate in the control.

Return Value

Type: System.Int32

The location within the control where the search text was found or -1 if the search string is not found or an empty search string is specified in the str parameter.

The Find method searches for the text specified in the str parameter and returns the location of the first character 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.


The Find methods that accept a string as a parameter cannot find text that is contained on more than one line of text within the RichTextBox. Performing such a search will return a value of negative one (-1).

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. If the search string is found in the RichTextBox, the method returns a value of true and highlights the search text, otherwise it returns false. The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1.

public bool FindMyText(string text)
   // Initialize the return value to false by default.
   bool returnValue = false;

   // Ensure a search string has been specified.
   if (text.Length > 0) 
      // Obtain the location of the search string in richTextBox1.
      int indexToText = richTextBox1.Find(text);
      // Determine whether the text was found in richTextBox1.
      if(indexToText >= 0)
         returnValue = true;

   return returnValue;

.NET Framework
Available since 1.1
Return to top