RichTextBox.Find RichTextBox.Find RichTextBox.Find RichTextBox.Find Method

定义

RichTextBox 的内容内搜索文本。Searches for text within the contents of the RichTextBox.

重载

Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件文本中搜索控件内某个文本范围内的字符串。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.

Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。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.

Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32)

RichTextBox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。Searches a range of text in a RichTextBox control for the first instance of a character from a list of characters.

Find(Char[]) Find(Char[]) Find(Char[]) Find(Char[])

RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。Searches the text of a RichTextBox control for the first instance of a character from a list of characters.

Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32)

从特定的起始点开始,在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。Searches the text of a RichTextBox control, at a specific starting point, for the first instance of a character from a list of characters.

Find(String) Find(String) Find(String) Find(String)

RichTextBox 控件的文本中搜索字符串。Searches the text in a RichTextBox control for a string.

Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。Searches the text in a RichTextBox control for a string with specific options applied to the search.

Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds) Find(String, Int32, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件文本中搜索控件内某个文本范围内的字符串。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.

public:
 int Find(System::String ^ str, int start, int end, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, int start, int end, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * int * int * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, start As Integer, end As Integer, options As RichTextBoxFinds) As Integer

参数

str
String String String String

要在控件中定位的文本。The text to locate in the control.

start
Int32 Int32 Int32 Int32

控件文本中开始搜索的位置。The location within the control's text at which to begin searching.

end
Int32 Int32 Int32 Int32

控件文本中结束搜索的位置。The location within the control's text at which to end searching. 此值必须等于 -1 或者大于或等于 start 参数。This value must be equal to negative one (-1) or greater than or equal to the start parameter.

options
RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds

RichTextBoxFinds 值的按位组合。A bitwise combination of the RichTextBoxFinds values.

返回

控件内找到搜索文本的位置。The location within the control where the search text was found.

异常

start 参数小于零。The start parameter was less than zero.

-or- end 参数小于 start 参数。The end parameter was less the start parameter.

示例

下面的代码示例在的文本部分中搜索传递RichTextBox searchText到该方法的参数中的搜索字符串的第一个实例。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. 要在控件中搜索文本的范围由searchStart方法的和searchEnd参数指定。The range to search for text within the control is specified by the searchStart and searchEnd parameters of the method. 如果在中RichTextBox找到了搜索字符串,则该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本; 否则,它将返回值-1。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. 该示例还使用options Find方法的参数指定所找到的文本应该与搜索字符串的大小写匹配。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. 该示例要求将此方法放置在Form RichTextBox包含名为richTextBox1的控件的类中。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;
   }
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;
}
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

注解

方法搜索str参数中指定的文本,并返回该搜索字符串在控件中的第一个字符的位置。 FindThe 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. 例如,如果用户在控件中输入了日期,则可以使用Find方法来搜索文档中的所有日期,并在SaveFile使用控件的方法之前将它们替换为适当的格式。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.

使用此版本的Find方法,可以指定使你可以扩展或缩小搜索范围的选项。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. RichTextBoxFinds.Reverse 如果options在参数中指定枚举,则可以在文档底部搜索文本,而不是默认的从上到下的搜索方法。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. 此版本的Find方法还允许您通过选择控件文本内的特定开始位置和结束位置来缩小文本搜索范围。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. 如果向end参数分配了负一(-1)的值,则该方法将RichTextBox在中的文本末尾处进行搜索,以便进行常规搜索。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. 对于反向搜索,分配给该end参数的值为负1(-1)表示将从文本末尾(底部)到start参数定义的位置搜索文本。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. 如果为end和参数提供的值相同,则会在整个控件中搜索常规搜索。 startWhen 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.

备注

接受作为参数的RichTextBox方法不能找到中多行文本中包含的文本。 Find stringThe 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. 执行这样的搜索将返回值为负1(-1)。Performing such a search will return a value of negative one (-1).

Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds) Find(String, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。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.

public:
 int Find(System::String ^ str, int start, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, int start, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * int * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, start As Integer, options As RichTextBoxFinds) As Integer

参数

str
String String String String

要在控件中定位的文本。The text to locate in the control.

start
Int32 Int32 Int32 Int32

控件文本中开始搜索的位置。The location within the control's text at which to begin searching.

options
RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds

RichTextBoxFinds 值的按位组合。A bitwise combination of the RichTextBoxFinds values.

返回

控件内找到搜索文本的位置。The location within the control where the search text was found.

示例

下面的代码示例在的全部内容RichTextBox中搜索传递到该方法的文本参数中的搜索字符串的第一个实例的。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. 搜索起始位置由方法的 start 参数指定。The search starting location is specified by the start parameter of the method. 如果在中RichTextBox找到了搜索字符串,则该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本; 否则,它将返回值-1。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 specifies options in the search to match the case of the specified search string. 该示例要求将此方法放置在Form RichTextBox包含名为richTextBox1的的类中。The example requires that this method is placed in the class of a Form that contains a RichTextBox named richTextBox1. 您可以使用此示例来执行 "查找下一个" 操作类型。You can use this example to perform a "Find Next" type of operation. 找到搜索文本的实例后,可以通过更改start参数的值来搜索当前匹配位置之外的位置,从而查找文本的其他实例。Once an instance of the search text has been found, you can find other instances of the text by changing the value of the start parameter to search at a location beyond the position of the current match.

public:
   int FindMyText( String^ text, int start )
   {
      // 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 && start >= 0 )
      {
         // Obtain the location of the search string in richTextBox1.
         int indexToText = richTextBox1->Find( text, start, RichTextBoxFinds::MatchCase );
         // Determine whether the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            returnValue = indexToText;
         }
      }

      return returnValue;
   }
public int FindMyText(string text, int start)
{
   // 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 && start >= 0) 
   {
      // Obtain the location of the search string in richTextBox1.
      int indexToText = richTextBox1.Find(text, start, RichTextBoxFinds.MatchCase);
      // Determine whether the text was found in richTextBox1.
      if(indexToText >= 0)
      {
         returnValue = indexToText;
      }
   }

   return returnValue;
}

Public Function FindMyText(text As String, start As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = - 1
    
    ' Ensure that a search string has been specified and a valid start point.
    If text.Length > 0 And start >= 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text, start, _
            RichTextBoxFinds.MatchCase)
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = indexToText
        End If
    End If
    
    Return returnValue
End Function

注解

方法搜索str参数中指定的文本,并返回该搜索字符串在控件中的第一个字符的位置。 FindThe 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. 例如,如果用户在控件中输入了日期,则可以使用Find方法在SaveFile使用控件的方法之前,搜索文档中的所有日期并将其替换为适当的格式。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.

使用此版本的Find方法,可以指定使你可以扩展或缩小搜索范围的选项。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. RichTextBoxFinds.Reverse 如果options在参数中指定枚举,则可以在文档底部搜索文本,而不是默认的从上到下的搜索方法。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. 此版本的Find方法还允许您通过选择控件文本内的特定起始位置来缩小文本搜索范围。This version of the Find method also enables you to narrow the search for text by selecting a specific starting position within the control's text. 此功能可以让你避免可能已搜索的文本或要搜索的特定文本是否不存在。This feature can enable you to avoid text that might have already been searched or where the specific text you are searching for is known not to exist. 如果在RichTextBoxFinds.Reverse options参数中指定了值start ,则参数的值指示将在使用此版本的时从文档底部开始搜索后将结束反向搜索的位置。Find方法。When the RichTextBoxFinds.Reverse value is specified in the options parameter, the value of the start parameter indicates the position where the reverse search will end since the search will start at the bottom of the document when using this version of the Find method.

备注

接受作为参数的RichTextBox方法不能找到中多行文本中包含的文本。 Find stringThe 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. 执行这样的搜索将返回值为负1(-1)。Performing such a search will return a value of negative one (-1).

Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32) Find(Char[], Int32, Int32)

RichTextBox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。Searches a range of text in a RichTextBox control for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet, int start, int end);
public int Find (char[] characterSet, int start, int end);
member this.Find : char[] * int * int -> int
Public Function Find (characterSet As Char(), start As Integer, end As Integer) As Integer

参数

characterSet
Char[]

要搜索的字符数组。The array of characters to search for.

start
Int32 Int32 Int32 Int32

控件文本中开始搜索的位置。The location within the control's text at which to begin searching.

end
Int32 Int32 Int32 Int32

控件文本中结束搜索的位置。The location within the control's text at which to end searching.

返回

控件内找到搜索字符的位置。The location within the control where the search characters are found.

异常

start 小于 0 或者大于控件中文本的长度。start is less than 0 or greater than the length of the text in the control.

注解

此版本的Find方法从characterSet参数中指定的字符列表搜索字符的第一个实例,并返回字符的位置。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. 例如,传递包含字符 "Q" 的字符数组。For example, you pass an array of characters containing the character 'Q'. 如果控件包含文本 "Quick 棕色 Fox",则该Find方法将返回值四。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. 如果找到方法的characterSet参数中提供的字符列表中的字符,则此方法返回的值是该字符在控件中的位置的从零开始的索引。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.

此版本的Find方法使你可以通过为startend参数指定值,在控件中的文本范围内搜索字符集。This version of the Find method enables you to search for a character set from a range of text in the control by specifying a value for the start and end parameters. 如果start参数的值为零,则指示应从控件文档的开头开始搜索。A value of zero for the start parameter indicates that the search should start from the beginning of the control's document. end参数的-1 值指示搜索应在控件中的文本的末尾结束。A -1 value for the end parameter indicates that the search should end at the end of the text within the control. 您可以使用此版本的Find方法将搜索范围缩小到控件内的特定文本范围,以避免搜索文档中不太重要的文档区域。You can use this version of the Find method to narrow your search to a specific range of text within the control to avoid searching areas of the document that are not important to the needs of your application.

Find(Char[]) Find(Char[]) Find(Char[]) Find(Char[])

RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。Searches the text of a RichTextBox control for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet);
public int Find (char[] characterSet);
member this.Find : char[] -> int
Public Function Find (characterSet As Char()) As Integer

参数

characterSet
Char[]

要搜索的字符数组。The array of characters to search for.

返回

在控件中找到搜索字符的位置;如果未找到搜索字符或者在 char 参数中指定了空搜索字符集,则为 -1。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.

示例

下面的代码示例在的内容RichTextBox text中搜索传递给参数中的方法的字符。The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. 如果在text RichTextBox中找到数组的内容,则该方法返回找到的值的索引; 否则返回-1。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. 该示例要求将此方法Form放置在RichTextBox包含名为richTextBox1的控件的类中,并将一个Buttonbutton1为的控件连接到Click在示例。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*/ )
   {
      array<Char>^temp1 = {'D','e','l','t','a'};
      MessageBox::Show( FindMyText( temp1 ).ToString() );
   }

public:
   int FindMyText( array<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(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 Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    MessageBox.Show(FindMyText(New Char() {"B"c, "r"c, "a"c, "v"c, "o"c}).ToString())
End Sub


Public Function FindMyText(ByVal [text]() As Char) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a search string has been specified and a valid start point.
    If [text].Length > 0 Then
        ' Obtain the location of the first character found in the control
        ' that matches any of the characters in the char array.
        Dim indexToText As Integer = richTextBox1.Find([text])
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            ' Return the location of the character.
            returnValue = indexToText
        End If
    End If

    Return returnValue
End Function

注解

此版本的Find方法从characterSet参数中指定的字符列表搜索字符的第一个实例,并返回字符的位置。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. 例如,传递包含字符 "Q" 的字符数组。For example, you pass an array of characters containing the character 'Q'. 如果控件包含文本 "Quick 棕色 Fox",则该Find方法将返回值四。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. 此版本的Find方法要求搜索包含在控件中的整个文档中的字符。This version of the Find method requires that the entire document contained in the control is searched for the characters. 如果找到方法的characterSet参数中提供的字符列表中的字符,则此方法返回的值是该字符在控件中的位置的从零开始的索引。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.

Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32) Find(Char[], Int32)

从特定的起始点开始,在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。Searches the text of a RichTextBox control, at a specific starting point, for the first instance of a character from a list of characters.

public:
 int Find(cli::array <char> ^ characterSet, int start);
public int Find (char[] characterSet, int start);
member this.Find : char[] * int -> int
Public Function Find (characterSet As Char(), start As Integer) As Integer

参数

characterSet
Char[]

要搜索的字符数组。The array of characters to search for.

start
Int32 Int32 Int32 Int32

控件文本中开始搜索的位置。The location within the control's text at which to begin searching.

返回

控件内找到搜索字符的位置。The location within the control where the search characters are found.

示例

下面的代码示例在的内容RichTextBox text中搜索传递给参数中的方法的字符。The following code example searches the contents of a RichTextBox for the characters that are passed to the method in the text parameter. 搜索从RichTextBox FindMyText方法的start参数指定的中的位置开始。The search begins from the location within the RichTextBox specified by the start parameter of the FindMyText method. 如果在中RichTextBox找到文本数组的内容,则方法返回找到的值的索引; 否则返回-1。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. 该示例要求将此方法放置在Form包含一个RichTextBox名为richTextBox1的控件的类中,并ClickButton一个名button1为的控件连接到在其中定义的事件处理程序实例.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*/ )
   {
      array<Char>^temp0 = {'B','r','a','v','o'};
      MessageBox::Show( FindMyText( temp0, 5 ).ToString() );
   }

public:
   int FindMyText( array<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;
   }
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;
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    MessageBox.Show(FindMyText(New Char() {"B"c, "r"c, "a"c, "v"c, "o"c}, 5).ToString())
End Sub


Public Function FindMyText(ByVal text() As Char, ByVal start As Integer) As Integer
    ' Initialize the return value to false by default.
    Dim returnValue As Integer = -1

    ' Ensure that a valid char array has been specified and a valid start point.
    If [text].Length > 0 And start >= 0 Then
        ' Obtain the location of the first character found in the control
        ' that matches any of the characters in the char array.
        Dim indexToText As Integer = richTextBox1.Find([text], start)
        ' Determine whether any of the chars are found in richTextBox1.
        If indexToText >= 0 Then
            ' Return the location of the character.
            returnValue = indexToText
        End If
    End If

    Return returnValue
End Function

注解

此版本的Find方法从characterSet参数中指定的字符列表搜索某个字符的第一个实例,并返回该字符的位置。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. 例如,传递包含字符 "Q" 的字符数组。For example, you pass an array of characters containing the character 'Q'. 如果控件包含文本 "Quick 棕色 Fox",则该Find方法将返回值四。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. 如果找到方法的characterSet参数中提供的字符列表中的字符,则此方法返回的值是该字符在控件中的位置的从零开始的索引。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.

此版本的Find方法使您可以通过指定start参数的值,在控件文本内的指定起始位置搜索字符集。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. 你可以使用此版本的Find方法来缩小搜索范围,以避免你知道的文本不包含你所搜索的指定字符,或在搜索中不重要。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.

Find(String) Find(String) Find(String) Find(String)

RichTextBox 控件的文本中搜索字符串。Searches the text in a RichTextBox control for a string.

public:
 int Find(System::String ^ str);
public int Find (string str);
member this.Find : string -> int
Public Function Find (str As String) As Integer

参数

str
String String String String

要在控件中定位的文本。The text to locate in the control.

返回

在控件中找到搜索文本的位置;如果未找到搜索字符串或者在 str 参数中指定了空搜索字符串,则为 -1。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.

示例

下面的代码示例在的全部内容RichTextBox中搜索传递到该方法的文本参数中的搜索字符串的第一个实例的。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. 如果在中RichTextBox找到了搜索字符串,则该方法返回的true值并突出显示搜索文本,否则返回falseIf the search string is found in the RichTextBox, the method returns a value of true and highlights the search text, otherwise it returns false. 该示例要求将此方法放置在Form RichTextBox包含名为richTextBox1的的类中。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;
   }
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;
}

Public Function FindMyText(text As String) As Boolean
    ' Initialize the return value to false by default.
    Dim returnValue As Boolean = False
    
    ' Ensure a search string has been specified.
    If text.Length > 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text)
        ' Determine whether the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = True
        End If
    End If
    
    Return returnValue
End Function

注解

方法搜索str参数中指定的文本,并返回控件中第一个字符的位置。 FindThe 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. 例如,如果用户在控件中输入了日期,则可以使用Find方法在SaveFile使用控件的方法之前,搜索文档中的所有日期并将其替换为适当的格式。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.

备注

接受作为参数的RichTextBox方法不能找到中多行文本中包含的文本。 Find stringThe 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. 执行这样的搜索将返回值为负1(-1)。Performing such a search will return a value of negative one (-1).

Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds) Find(String, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。Searches the text in a RichTextBox control for a string with specific options applied to the search.

public:
 int Find(System::String ^ str, System::Windows::Forms::RichTextBoxFinds options);
public int Find (string str, System.Windows.Forms.RichTextBoxFinds options);
member this.Find : string * System.Windows.Forms.RichTextBoxFinds -> int
Public Function Find (str As String, options As RichTextBoxFinds) As Integer

参数

str
String String String String

要在控件中定位的文本。The text to locate in the control.

options
RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds RichTextBoxFinds

RichTextBoxFinds 值的按位组合。A bitwise combination of the RichTextBoxFinds values.

返回

控件内找到搜索文本的位置。The location within the control where the search text was found.

示例

下面的代码示例在的全部内容RichTextBox中搜索传递到该方法的文本参数中的搜索字符串的第一个实例的。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. 如果在中RichTextBox找到了搜索字符串,则该方法将返回的true值并突出显示文本; 否则,将返回falseIf the search string is found in the RichTextBox, the method returns a value of true and highlights the text; otherwise, it returns false. 该示例还在搜索中指定了与指定的搜索字符串匹配的选项。The example also specifies options in the search to match the case of the specified search string. 该示例要求将此方法放置在Form RichTextBox包含名为richTextBox1的的类中。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, RichTextBoxFinds::MatchCase );
         // Determine if the text was found in richTextBox1.
         if ( indexToText >= 0 )
         {
            returnValue = true;
         }
      }

      return returnValue;
   }
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, RichTextBoxFinds.MatchCase);
      // Determine if the text was found in richTextBox1.
      if(indexToText >= 0)
      {
         returnValue = true;
      }
   }

   return returnValue;
}

Public Function FindMyText(text As String) As Boolean
    ' Initialize the return value to false by default.
    Dim returnValue As Boolean = False
    
    ' Ensure a search string has been specified.
    If text.Length > 0 Then
        ' Obtain the location of the search string in richTextBox1.
        Dim indexToText As Integer = richTextBox1.Find(text, RichTextBoxFinds.MatchCase)
        ' Determine if the text was found in richTextBox1.
        If indexToText >= 0 Then
            returnValue = True
        End If
    End If
    
    Return returnValue
End Function

注解

方法搜索str参数中指定的文本,并返回控件中第一个字符的位置。 FindThe 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. 例如,如果用户在控件中输入了日期,则可以使用Find方法来搜索文档中的所有日期,并在SaveFile使用控件的方法之前将它们替换为适当的格式。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.

使用此版本的Find方法,可以指定使你可以扩展或缩小搜索范围的选项。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. RichTextBoxFinds.Reverse 如果options在参数中指定枚举,则可以在文档底部搜索文本,而不是默认的从上到下的搜索方法。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.

备注

接受作为参数的RichTextBox方法不能找到中多行文本中包含的文本。 Find stringThe 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. 执行这样的搜索将返回值为负1(-1)。Performing such a search will return a value of negative one (-1).

适用于