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 的控制項,以及 Button 名為 的控制項,該 button1 控制項會連接到 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 則方法會傳回四個值。 大寫字元和小寫字元會被視為搜尋中的不同值。

如果屬性傳回負值,則搜尋的字元在 控制項的內容中找不到。 您可以使用這個方法來搜尋 控制項內的一組字元。 這個版本的 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。

範例

下列程式碼範例會針對傳遞至 方法之 text 參數之搜尋字串的第一個實例,搜尋 的整個內容 RichTextBox 。 如果在 中找到 RichTextBox 搜尋字串,則方法會傳回 的值 true 並反白顯示搜尋文字,否則會傳 false 回 。 此範例會要求這個方法放在 包含具名 richTextBox1 的 類別 FormRichTextBox 中。

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 字元。 搜尋會從 方法的 參數 FindMyTextstart 指定的 位置 RichTextBox 開始。 如果在 中找到 RichTextBox 文字陣列的內容,則方法會傳回找到之值的索引,否則會傳回 -1。 此範例會要求這個方法放在 的 類別中,其中包含名為 的 Form 控制項,以及名為 的控制項,且 Button 該控制項會 button1 連接到 Click 範例中所定義的 richTextBox1 事件處理常式。 RichTextBox

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 則方法會傳回四個值。 大寫字元和小寫字元會被視為搜尋中的不同值。

如果屬性傳回負值,則搜尋的字元在 控制項的內容中找不到。 您可以使用這個方法來搜尋 控制項內的一組字元。 如果找到方法 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 值的位元組合。

傳回

控制項中找到搜尋文字的位置。

範例

下列程式碼範例會針對傳遞至 方法之 text 參數之搜尋字串的第一個實例,搜尋 的整個內容 RichTextBox 。 如果在 中找到 RichTextBox 搜尋字串,則方法會傳回 的值 true 並反白顯示文字,否則會傳 false 回 。 此範例也會指定搜尋中的選項,以符合指定搜尋字串的大小寫。 此範例會要求這個方法放在 包含具名 richTextBox1 的 類別 FormRichTextBox 中。

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 小於零或大於控制項中文字的長度。

備註

這個版本的 Find 方法會從 參數中指定的 characterSet 字元清單中搜尋字元的第一個實例,並傳回字元的位置。 例如,您可以傳遞包含字元 'Q' 的字元陣列。 如果控制項包含 「The Quick Brown Fox」 文字, Find 則方法會傳回四個值。 大寫字元和小寫字元會被視為搜尋中的不同值。

如果屬性傳回負值,則搜尋的字元在 控制項的內容中找不到。 您可以使用這個方法來搜尋 控制項內的一組字元。 如果找到方法 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 值的位元組合。

傳回

控制項中找到搜尋文字的位置。

範例

下列程式碼範例會針對傳遞至 方法之 text 參數之搜尋字串的第一個實例,搜尋 的整個內容 RichTextBox 。 搜尋起始位置是由 方法的 start 參數所指定。 如果在 中找到 RichTextBox 搜尋字串,則 方法會傳回所找到文字之第一個字元的索引位置,並反白顯示找到的文字,否則會傳回 -1 的值。 此範例也會指定搜尋中的選項,以符合指定之搜尋字串的大小寫。 此範例會要求這個方法放在 包含具名 richTextBox1FormRichTextBox 類別中。 您可以使用此範例來執行「尋找下一步」作業類型。 找到搜尋文字的實例之後,您可以藉由變更 參數的值 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 參數小於 0。

-或-

end 參數小於 start 參數。

範例

下列程式碼範例會搜尋 中 RichTextBox 文字的區段,以取得傳遞至 searchText 方法參數之搜尋字串的第一個實例。 要搜尋控制項內文字的範圍是由 searchStart 方法的 和 searchEnd 參數所指定。 如果在 中找到 RichTextBox 搜尋字串,則 方法會傳回所找到文字之第一個字元的索引位置,並反白顯示找到的文字,否則會傳回 -1 的值。 此範例也會使用 options 方法的 Find 參數來指定找到的文字應該符合搜尋字串的大小寫。 此範例會要求這個方法放在 的 類別中,其中包含 RichTextBox 名為 richTextBox1Form 控制項。 找到搜尋字串的第一個實例之後,您可以使用這個範例來尋找文字中的其他實例。

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) ,則方法會搜尋直到 中 RichTextBox 文字結尾的一般搜尋為止。 針對反向搜尋,指派給 end 參數的負值 (-1) 表示將從文字結尾搜尋文字, (底端) 參數所 start 定義的位置。 start當 和 end 參數提供相同的值時,會搜尋整個控制項的一般搜尋。 針對反向搜尋,會搜尋整個控制項,但搜尋會從檔底部開始,並搜尋至檔頂端。

注意

Find接受 string 做為參數的方法,在 內的一行以上的文字上 RichTextBox 找不到包含的文字。 執行這類搜尋會傳回負值 1 (-1) 。

適用於