This documentation is archived and is not being maintained.

RichTextBox.Find Method (Char[], Int32)

Searches the text of a RichTextBox control, at a specific starting point, for the first instance of a character from a list of characters.

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

public int Find(
	char[] characterSet,
	int start


Type: System.Char[]
The array of characters to search for.
Type: System.Int32
The location within the control's text at which to begin searching.

Return Value

Type: System.Int32
The location within the control where the search characters are found.

This version of the Find method searches for the first instance of a character from a list of characters specified in the characterSet parameter and returns the location the character. For example, you pass an array of characters containing the character 'Q'. If the control contained the text "The Quick Brown Fox", the Find method would return the value of four. An upper case character and a lower case character are considered different values in the search.

If the property returns a negative value, the characters being searched for were not found within the contents of the control. You can use this method to search for a group of characters within the control. If a character from the character list provided in the method's characterSet parameter is found, the value returned by this method is a zero-based index of the character's position in the control. A space is considered a character by the method when determining the location of a character.

This version of the Find method enables you to search for a character set from a specified start position within the text of the control by specifying a value for the start parameter. A value of zero indicates that the search should start from the beginning of the control's document. You can use this version of the Find method to narrow your search to avoid text that you already know does not contain the specified characters you are searching for or are not important in your search.

The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. The search begins from the location within the RichTextBox specified by the start parameter of the FindMyText method. If the contents of the text array are found in the RichTextBox, the method returns the index of the value that is found; otherwise, it returns -1. The example requires that this method is placed in the class of a Form that contains a RichTextBox control named richTextBox1 and a Button control named button1 that is connected to the Click event handler defined in the example.

		private void button1_Click(object sender, System.EventArgs e)
			MessageBox.Show(FindMyText(new char[]{'B','r','a','v','o'}, 5).ToString());

		public int FindMyText(char[] text, int start)
			// Initialize the return value to false by default.
			int returnValue = -1;

			// Ensure that a valid char array has been specified and a valid start point.
			if (text.Length > 0 && start >= 0) 
				// Obtain the location of the first character found in the control
				// that matches any of the characters in the char array.
				int indexToText = richTextBox1.Find(text, start);
				// Determine whether any of the chars are found in richTextBox1.
				if(indexToText >= 0)
					// Return the location of the character.
					returnValue = indexToText;

			return returnValue;

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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