Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

RichTextBox.Find Method (String, Int32, Int32, RichTextBoxFinds)

Searches the text in a RichTextBox control for a string within a range of text within the control and with specific options applied to the search.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public int Find(
	string str,
	int start,
	int end,
	RichTextBoxFinds options
)

Parameters

str
Type: System.String

The text to locate in the control.

start
Type: System.Int32

The location within the control's text at which to begin searching.

end
Type: System.Int32

The location within the control's text at which to end searching. This value must be equal to negative one (-1) or greater than or equal to the start parameter.

options
Type: System.Windows.Forms.RichTextBoxFinds

A bitwise combination of the RichTextBoxFinds values.

Return Value

Type: System.Int32
The location within the control where the search text was found.
ExceptionCondition
ArgumentNullException

The str parameter was null.

ArgumentException

The start parameter was less than zero.

ArgumentException

The end parameter was less the start parameter.

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 can 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 start and end position within the control's text. This feature can enable you to limit the search range to a specific section of the control's text. If a value of negative one (-1) is assigned to the end parameter, the method will search until the end of the text in the RichTextBox for normal searches. For reverse searches, a value of negative one (-1) assigned to the end parameter indicates that text will be searched from the end of text (bottom) to the position defined by start parameter. When the start and end parameters are provided the same value the entire control is searched for normal searches. For a reverse search, the entire control is searched but the search begins at the bottom of the document and searches to the top of the document.

NoteNote

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 a section of text in a RichTextBox for the first instance of a search string passed into the searchText parameter of the method. The range to search for text within the control is specified by the searchStart and searchEnd parameters 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 uses the options parameter of the Find method to specify that the found text should match the case of the search string. The example requires that this method is placed in the class of a Form that contains a RichTextBox control named richTextBox1. After the first instance of the search string has been found, you can use this example to find other instances in the text.

		public int FindMyText(string searchText, int searchStart, int searchEnd)
		{
			// Initialize the return value to false by default. 
			int returnValue = -1;

			// Ensure that a search string and a valid starting point are specified. 
			if (searchText.Length > 0 && searchStart >= 0) 
			{
				// Ensure that a valid ending value is provided. 
				if (searchEnd > searchStart || searchEnd == -1)
				{	
					// Obtain the location of the search string in richTextBox1. 
					int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
					// Determine whether the text was found in richTextBox1. 
					if(indexToText >= 0)
					{
						// Return the index to the specified search text.
						returnValue = indexToText;
					}
				}
			}

			return returnValue;
		}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.