EN
Il contenuto non è disponibile nella lingua di destinazione, ma solo in inglese.

RichTextBox.Find Method

Searches for text within the contents of the RichTextBox.

Overload List

Searches the text of a RichTextBox control for the first instance of a character from a list of characters.

[Visual Basic] Overloads Public Function Find(Char()) As Integer
[C#] public int Find(char[]);
[C++] public: int Find(__wchar_t __gc[]);
[JScript] public function Find(Char[]) : int;

Searches the text in a RichTextBox control for a string.

[Visual Basic] Overloads Public Function Find(String) As Integer
[C#] public int Find(string);
[C++] public: int Find(String*);
[JScript] public function Find(String) : int;

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

[Visual Basic] Overloads Public Function Find(Char(), Integer) As Integer
[C#] public int Find(char[], int);
[C++] public: int Find(__wchar_t __gc[], int);
[JScript] public function Find(Char[], int) : int;

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

[Visual Basic] Overloads Public Function Find(String, RichTextBoxFinds) As Integer
[C#] public int Find(string, RichTextBoxFinds);
[C++] public: int Find(String*, RichTextBoxFinds);
[JScript] public function Find(String, RichTextBoxFinds) : int;

Searches a range of text in a RichTextBox control for the first instance of a character from a list of characters.

[Visual Basic] Overloads Public Function Find(Char(), Integer, Integer) As Integer
[C#] public int Find(char[], int, int);
[C++] public: int Find(__wchar_t __gc[], int, int);
[JScript] public function Find(Char[], int, int) : int;

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.

[Visual Basic] Overloads Public Function Find(String, Integer, RichTextBoxFinds) As Integer
[C#] public int Find(string, int, RichTextBoxFinds);
[C++] public: int Find(String*, int, RichTextBoxFinds);
[JScript] public function Find(String, int, RichTextBoxFinds) : int;

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.

[Visual Basic] Overloads Public Function Find(String, Integer, Integer, RichTextBoxFinds) As Integer
[C#] public int Find(string, int, int, RichTextBoxFinds);
[C++] public: int Find(String*, int, int, RichTextBoxFinds);
[JScript] public function Find(String, int, int, RichTextBoxFinds) : int;

Example

[Visual Basic, C#, C++] The following 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 assumes 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.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Find. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Function FindMyText(ByVal searchText As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a search string and a valid starting point are specified.
    If searchText.Length > 0 And searchStart >= 0 Then
        ' Ensure that a valid ending value is provided.
        If searchEnd > searchStart Or searchEnd = -1 Then
            ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase)
            ' Determine whether the text was found in richTextBox1.
            If indexToText >= 0 Then
                ' Return the index to the specified search text.
                returnValue = indexToText
            End If
        End If
    End If

    Return returnValue
End Function

[C#] 
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;
}

[C++] 
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;
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

RichTextBox Class | RichTextBox Members | System.Windows.Forms Namespace

Mostra: