This documentation is archived and is not being maintained.

RichTextBox.Find Method (Char[])

Searches the text of a RichTextBox control 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
)
public int Find (
	char[] characterSet
)
public function Find (
	characterSet : char[]
) : int

Parameters

characterSet

The array of characters to search for.

Return Value

The location within the control where the search characters were found or -1 if the search characters are not found or an empty search character set is specified in the char parameter.

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 of 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. This version of the Find method requires that the entire document contained in the control is searched for the characters. 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.

The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. 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[]{'D','e','l','t','a'}).ToString());
}

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

    // Ensure that a search string has been specified and a valid start point.
    if (text.Length > 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);
        // Determine whether the text was found in richTextBox1.
        if(indexToText >= 0)
        {
            // Return the location of the character.
            returnValue = indexToText;
        }
    }

    return returnValue;
}

private void button1_Click(Object sender, System.EventArgs e)
{
    MessageBox.Show(((System.Int32)FindMyText(new char[] { 'D', 'e', 'l', 
        't', 'a' })).ToString());
} //button1_Click

public int FindMyText(char text[])
{
    // Initialize the return value to false by default.
    int returnValue = -1;
    // Ensure that a search string has been specified and a valid start point.
    if (text.get_Length() > 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);
        // Determine whether the text was found in richTextBox1.
        if (indexToText >= 0) {
            // Return the location of the character.
            returnValue = indexToText;
        }
    }
    return returnValue;
} //FindMyText

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: