RichTextBox.Find 方法

定义

RichTextBox 的内容内搜索文本。

重载

Find(Char[])

RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。

Find(String)

RichTextBox 控件的文本中搜索字符串。

Find(Char[], Int32)

从特定的起始点开始,在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。

Find(String, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。

Find(Char[], Int32, Int32)

RichTextBox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。

Find(String, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。

Find(String, Int32, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件文本中搜索控件内某个文本范围内的字符串。

Find(Char[])

RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。

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[]

要搜索的字符数组。

返回

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

示例

下面的代码示例在 RichTextBox 中搜索 的内容,查找在 参数中传递给 方法的 text 字符。 如果在 中找到RichTextBox数组的内容text,则 方法返回找到的值的索引;否则返回 -1。 该示例要求将此方法放置在 的 类中, Form 该类包含 RichTextBox 名为 的 richTextBox1 控件和一个名为 Buttonbutton1的控件,该控件连接到 Click 示例中定义的事件处理程序。

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 字符列表中搜索字符的第一个实例,并返回该字符的位置。 例如,传递包含字符“Q”的字符数组。 如果控件包含文本“The Quick Brown Fox”,则 Find 方法将返回值 4。 在搜索中,将大写字符和小写字符视为不同的值。

如果 属性返回负值,则搜索的字符在控件的内容中找不到。 可以使用此方法在 控件中搜索一组字符。 此方法的 Find 此版本要求搜索控件中包含的整个文档以查找字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是字符在控件中位置的从零开始的索引。 在确定字符的位置时,方法将空格视为字符。

适用于

Find(String)

RichTextBox 控件的文本中搜索字符串。

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

要在控件中定位的文本。

返回

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

示例

下面的代码示例搜索 的 整个内容 RichTextBox ,以查找传递到 方法的文本参数中的搜索字符串的第一个实例。 如果在 中找到 RichTextBox搜索字符串,该方法将返回 值 true 并突出显示搜索文本,否则返回 false。 该示例要求将此方法放置在包含RichTextBox名为 richTextBox1Form 类中。

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

注解

方法 Find 搜索 参数中指定的 str 文本,并返回控件中第一个字符的位置。 如果 属性返回负值,则搜索的文本字符串在控件的内容中找不到。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在 控件中输入了日期,则可以使用 Find 方法搜索文档中的所有日期,并在使用 SaveFile 控件的 方法之前将它们替换为适当的格式。

注意

Find接受 string 作为参数的方法找不到包含在 中的多行文本上的RichTextBox文本。 执行此类搜索将返回负值 1 (-1) 。

适用于

Find(Char[], Int32)

从特定的起始点开始,在 RichTextBox 控件的文本中搜索字符列表中某个字符的第一个实例。

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[]

要搜索的字符数组。

start
Int32

控件文本中开始搜索的位置。

返回

控件内找到搜索字符的位置。

示例

下面的代码示例在 RichTextBox 中搜索 的内容,查找在 参数中传递给 方法的 text 字符。 搜索从 方法的 参数FindMyText指定的 startRichTextBox的位置开始。 如果在 中找到 RichTextBox文本数组的内容,则 方法返回找到的值的索引;否则,它将返回 -1。 本示例要求将此方法放置在 的 类中, Form 该类包含 RichTextBox 名为 的 richTextBox1 控件和 Button 连接到示例中定义的事件处理程序的名为 button1Click 的控件。

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 字符列表中搜索字符的第一个实例,并返回该字符的位置。 例如,传递包含字符“Q”的字符数组。 如果控件包含文本“The Quick Brown Fox”,则 Find 方法将返回值 4。 在搜索中,将大写字符和小写字符视为不同的值。

如果 属性返回负值,则搜索的字符在控件的内容中找不到。 可以使用此方法在 控件中搜索一组字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是字符在控件中位置的从零开始的索引。 在确定字符的位置时,方法将空格视为字符。

通过此版本的 Find 方法,可以通过为 start 参数指定值,从控件文本中的指定起始位置搜索字符集。 值为零表示搜索应从控件文档的开头开始。 可以使用此版本的 Find 方法缩小搜索范围,以避免已知不包含要搜索的指定字符或在搜索中不重要的文本。

适用于

Find(String, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索字符串。

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

要在控件中定位的文本。

options
RichTextBoxFinds

RichTextBoxFinds 值的按位组合。

返回

控件内找到搜索文本的位置。

示例

下面的代码示例搜索 的 整个内容 RichTextBox ,以查找传递到 方法的文本参数中的搜索字符串的第一个实例。 如果在 中找到 RichTextBox搜索字符串,则 方法返回 值 true 并突出显示文本;否则,它将返回 false。 该示例还指定搜索中的选项,以匹配指定搜索字符串的事例。 该示例要求将此方法放置在包含RichTextBox名为 richTextBox1Form 类中。

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

注解

方法 Find 搜索 参数中指定的 str 文本,并返回控件中第一个字符的位置。 如果 属性返回负值,则搜索的文本字符串在控件的内容中找不到。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在 控件中输入了日期,则可以使用 Find 方法搜索文档中的所有日期,并在使用 SaveFile 控件的 方法之前将它们替换为适当的格式。

使用此版本的 Find 方法,可以指定使你能够扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词而不是部分字词。 通过在 参数中options指定RichTextBoxFinds.Reverse枚举,可以搜索从文档底部到顶部的文本,而不是默认的从上到下搜索方法。

注意

Find接受 string 作为参数的方法找不到包含在 中的多行文本上的RichTextBox文本。 执行此类搜索将返回负值 1 (-1) 。

适用于

Find(Char[], Int32, Int32)

RichTextBox 控件的某个文本范围中搜索字符列表的某个字符的第一个实例。

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[]

要搜索的字符数组。

start
Int32

控件文本中开始搜索的位置。

end
Int32

控件文本中结束搜索的位置。

返回

控件内找到搜索字符的位置。

例外

characterSet 为 null。

start 小于 0 或者大于控件中文本的长度。

注解

此版本的 Find 方法从 参数中指定的 characterSet 字符列表中搜索字符的第一个实例,并返回该字符的位置。 例如,传递包含字符“Q”的字符数组。 如果控件包含文本“The Quick Brown Fox”,则 Find 方法将返回值 4。 在搜索中,将大写字符和小写字符视为不同的值。

如果 属性返回负值,则搜索的字符在控件的内容中找不到。 可以使用此方法在 控件中搜索一组字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是字符在控件中位置的从零开始的索引。 在确定字符的位置时,方法将空格视为字符。

通过此版本的 Find 方法,可以通过为 和 end 参数指定值start,从控件中的文本范围中搜索字符集。 参数的值为零 start 表示搜索应从控件文档的开头开始。 参数的 end -1 值指示搜索应在控件中的文本末尾结束。 可以使用此版本的 Find 方法将搜索范围缩小到控件中的特定文本范围,以避免搜索对应用程序需求不重要的文档区域。

适用于

Find(String, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件的文本中搜索位于控件内特定位置的字符串。

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

要在控件中定位的文本。

start
Int32

控件文本中开始搜索的位置。

options
RichTextBoxFinds

RichTextBoxFinds 值的按位组合。

返回

控件内找到搜索文本的位置。

示例

下面的代码示例搜索 的 整个内容 RichTextBox ,以查找传递到 方法的文本参数中的搜索字符串的第一个实例。 搜索起始位置由 方法的 start 参数指定。 如果在 中找到 RichTextBox搜索字符串,该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本;否则,它将返回值 -1。 该示例还指定搜索中的选项,以匹配指定搜索字符串的事例。 该示例要求将此方法放置在包含RichTextBox名为 richTextBox1Form 类中。 可以使用此示例执行“查找下一个”类型的操作。 找到搜索文本的实例后,可以通过更改 参数的值 start 以在当前匹配项位置以外的位置进行搜索来查找文本的其他实例。

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

注解

方法 Find 搜索 参数中指定的 str 文本,并返回控件中搜索字符串的第一个字符的位置。 如果 属性返回负值,则搜索的文本字符串在控件的内容中找不到。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在 控件中输入了日期,则可以使用 Find 方法搜索文档中的所有日期,并在使用 SaveFile 控件的 方法之前将它们替换为适当的格式。

使用此版本的 Find 方法,可以指定使你能够扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词而不是部分字词。 通过在 参数中options指定RichTextBoxFinds.Reverse枚举,可以搜索从文档底部到顶部的文本,而不是默认的从上到下搜索方法。 此版本的 Find 方法还允许您通过选择控件文本中的特定起始位置来缩小文本搜索范围。 此功能可避免可能已搜索的文本,或者要搜索的特定文本已知不存在。 RichTextBoxFinds.Reverse在 参数中options指定值时,参数的值start指示反向搜索将结束的位置,因为使用此版本的 Find 方法时,搜索将从文档底部开始。

注意

Find接受 string 作为参数的方法找不到包含在 中的多行文本上的RichTextBox文本。 执行此类搜索将返回负值 1 (-1) 。

适用于

Find(String, Int32, Int32, RichTextBoxFinds)

在对搜索应用特定选项的情况下,在 RichTextBox 控件文本中搜索控件内某个文本范围内的字符串。

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

要在控件中定位的文本。

start
Int32

控件文本中开始搜索的位置。

end
Int32

控件文本中结束搜索的位置。 此值必须等于 -1 或者大于或等于 start 参数。

options
RichTextBoxFinds

RichTextBoxFinds 值的按位组合。

返回

控件内找到搜索文本的位置。

例外

str 参数是 null

start 参数小于零。

- 或 -

end 参数小于 start 参数。

示例

下面的代码示例在 中 RichTextBox 搜索文本部分,以获取传递到 searchText 方法 参数的搜索字符串的第一个实例。 在 控件中搜索文本的范围由 searchStart 方法的 和 searchEnd 参数指定。 如果在 中找到 RichTextBox搜索字符串,该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本;否则,它将返回值 -1。 该示例还使用 options 方法的 Find 参数来指定找到的文本应与搜索字符串的大小写匹配。 该示例要求将此方法放置在 包含RichTextBox名为 richTextBox1的 控件的 Form 类中。 找到搜索字符串的第一个实例后,可以使用此示例在文本中查找其他实例。

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

注解

方法 Find 搜索 参数中指定的 str 文本,并返回控件中搜索字符串的第一个字符的位置。 如果 属性返回负值,则搜索的文本字符串在控件的内容中找不到。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在 控件中输入了日期,则可以使用 Find 方法搜索文档中的所有日期,并在使用 SaveFile 控件的 方法之前将它们替换为适当的格式。

使用此版本的 Find 方法,可以指定使你能够扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词而不是部分字词。 通过在 参数中options指定RichTextBoxFinds.Reverse枚举,可以搜索从文档底部到顶部的文本,而不是默认的从上到下搜索方法。 此版本的 Find 方法还允许您通过在控件的文本中选择特定的开始和结束位置来缩小文本搜索范围。 使用此功能可将搜索范围限制为控件文本的特定部分。 如果为 参数分配 end 了负 1 (-1) 值,则 方法将搜索到 中 RichTextBox 文本的末尾,以便进行正常搜索。 对于反向搜索,分配给 end 参数) (-1 负值表示将从文本末尾 (底部) 搜索文本到参数定义 start 的位置。 start当为 和 end 参数提供相同的值时,将搜索整个控件进行常规搜索。 对于反向搜索,将搜索整个控件,但搜索从文档底部开始,并搜索到文档顶部。

注意

Find接受 string 作为参数的方法找不到包含在 中的多行文本上的RichTextBox文本。 执行此类搜索将返回负值 1 (-1) 。

适用于