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 検索します。 配列の text 内容が で RichTextBox見つかった場合、メソッドは見つかった値のインデックスを返します。それ以外の場合は -1 を返します。 この例では、このメソッドは、 という名前のコントロールとButton、 という名前richTextBox1Formコントロールを含む RichTextBox のクラスに配置され、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 メソッドは 4 の値を返します。 検索では、大文字と小文字が異なる値と見なされます。

プロパティが負の値を返す場合、検索対象の文字がコントロールの内容内に見つかりませんでした。 このメソッドを使用して、コントロール内の文字のグループを検索できます。 このバージョンの メソッドでは Find 、コントロールに含まれるドキュメント全体で文字を検索する必要があります。 メソッド characterSet のパラメーターで指定された文字リストの文字が見つかった場合、このメソッドによって返される値は、コントロール内の文字の位置の 0 から始まるインデックスです。 スペースは、文字の場所を決定するときに、 メソッドによって文字と見なされます。

適用対象

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 text パラメーターに渡された検索文字列の最初のインスタンスについて、 の内容全体を検索します。 で検索文字列が見つかった RichTextBox場合、 メソッドは の true 値を返し、検索テキストを強調表示します。それ以外の場合は を返します false。 この例では、このメソッドを、 という名前richTextBox1の を含む のFormクラスにRichTextBox配置する必要があります。

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 検索し、コントロール内の最初の文字の位置を返します。 プロパティが負の値を返す場合、検索対象のテキスト文字列がコントロールの内容内に見つかりませんでした。 このメソッドを使用して、コントロールのユーザーに提供できる検索機能を作成できます。 このメソッドを使用して、特定の形式に置き換えるテキストを検索することもできます。 たとえば、ユーザーがコントロールに日付を入力した場合、コントロールの メソッドを使用する前SaveFileに、 メソッドをFind使用してドキュメント内のすべての日付を検索し、適切な形式に置き換えることができます。

注意

パラメーターとして をstring受け取るメソッドはFind、 内の複数のテキスト行に含まれるテキストを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 検索します。 検索は、 メソッドの パラメーターでstart指定された 内RichTextBoxFindMyText場所から開始されます。 テキスト配列の内容が で RichTextBox見つかった場合、メソッドは見つかった値のインデックスを返します。それ以外の場合は 、-1 を返します。 この例では、このメソッドは、 という名前richTextBox1Formコントロールと、この例で定義されているイベント ハンドラーにClick接続されている という名前button1のコントロールをButton含む 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 メソッドは 4 の値を返します。 検索では、大文字と小文字が異なる値と見なされます。

プロパティが負の値を返す場合、検索対象の文字がコントロールの内容内に見つかりませんでした。 このメソッドを使用して、コントロール内の文字のグループを検索できます。 メソッド characterSet のパラメーターで指定された文字リストの文字が見つかった場合、このメソッドによって返される値は、コントロール内の文字の位置の 0 から始まるインデックスです。 スペースは、文字の場所を決定するときに、 メソッドによって文字と見なされます。

このバージョンの Find メソッドを使用すると、 パラメーターの値を指定することで、コントロールのテキスト内の指定した開始位置から文字セットを start 検索できます。 値 0 は、コントロールのドキュメントの先頭から検索を開始する必要があることを示します。 このバージョンの メソッドを 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 text パラメーターに渡された検索文字列の最初のインスタンスについて、 の内容全体を検索します。 で検索文字列が見つかった RichTextBox場合、メソッドは の true 値を返し、テキストを強調表示します。それ以外の場合は を返します false。 この例では、指定した検索文字列の大文字と小文字を一致させる検索のオプションも指定します。 この例では、このメソッドを、 という名前richTextBox1の を含む のFormクラスにRichTextBox配置する必要があります。

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 検索し、コントロール内の最初の文字の位置を返します。 プロパティが負の値を返す場合、検索対象のテキスト文字列がコントロールの内容内に見つかりませんでした。 このメソッドを使用して、コントロールのユーザーに提供できる検索機能を作成できます。 このメソッドを使用して、特定の形式に置き換えるテキストを検索することもできます。 たとえば、ユーザーがコントロールに日付を入力した場合、コントロールの メソッドを使用する前SaveFileに、 メソッドをFind使用してドキュメント内のすべての日付を検索し、適切な形式に置き換えることができます。

このバージョンの Find メソッドでは、検索を拡張または絞り込むことができるオプションを指定できます。 検索語の大文字と小文字を一致させたり、部分的な単語ではなく単語全体を検索したりするためのオプションを指定できます。 パラメーターで options 列挙をRichTextBoxFinds.Reverse指定すると、既定の top から bottom の検索メソッドではなく、ドキュメントの下部から先頭までのテキストを検索できます。

注意

パラメーターとして をstring受け取るメソッドはFind、 内の複数のテキスト行に含まれるテキストを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 のパラメーターで指定された文字リストの文字が見つかった場合、このメソッドによって返される値は、コントロール内の文字の位置の 0 から始まるインデックスです。 スペースは、文字の場所を決定するときに、 メソッドによって文字と見なされます。

このバージョンの Find メソッドを使用すると、 パラメーターと end パラメーターの値を指定することで、コントロール内のテキスト範囲から文字セットをstart検索できます。 パラメーターの値が start 0 の場合は、コントロールのドキュメントの先頭から検索を開始する必要があることを示します。 パラメーターの -1 値は end 、コントロール内のテキストの末尾で検索を終了することを示します。 このバージョンの メソッドを 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 text パラメーターに渡された検索文字列の最初のインスタンスについて、 の内容全体を検索します。 検索の開始位置は、 メソッドの start パラメーターで指定します。 で RichTextBox検索文字列が見つかった場合、 メソッドは、見つかったテキストの最初の文字のインデックス位置を返し、見つかったテキストを強調表示します。それ以外の場合は、-1 の値を返します。 この例では、指定した検索文字列の大文字と小文字を一致させる検索のオプションも指定します。 この例では、このメソッドを、 という名前richTextBox1の を含む のFormクラスにRichTextBox配置する必要があります。 この例を使用すると、"次を検索" の種類の操作を実行できます。 検索テキストのインスタンスが見つかったら、パラメーターの値 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.Reverseoptions指定すると、 パラメーターの値は、このバージョンの start メソッドを使用するとドキュメントの下部から検索が開始されるため、逆引き検索が終了する位置をFind示します。

注意

パラメーターとして をstring受け取るメソッドではFind、 内の複数行のテキストに含まれるテキストをRichTextBox見つけることができません。 このような検索を実行すると、負の値 (-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 パラメーターを使用して、見つかったテキストが検索文字列の大文字と小文字を一致させる必要があることを指定します。 この例では、 という名前richTextBox1のコントロールを含む のFormクラスにこのメソッドをRichTextBox配置する必要があります。 検索文字列の最初のインスタンスが見つかったら、この例を使用してテキスト内の他のインスタンスを検索できます。

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 メソッドを使用すると、コントロールのテキスト内の特定の開始位置と終了位置を選択して、テキストの検索を絞り込むこともできます。 この機能を使用すると、検索範囲をコントロールのテキストの特定のセクションに制限できます。 負の 1 (-1) の値が パラメーターに end 割り当てられている場合、メソッドは、 内 RichTextBox のテキストの末尾まで通常の検索を検索します。 逆引き検索の場合、パラメーターに負の 1 (-1) の値が割り当てられる end と、テキストがテキストの末尾 (下部) からパラメーターで start 定義された位置まで検索されることを示します。 パラメーターと end パラメーターがstart同じ値を指定すると、コントロール全体が通常の検索で検索されます。 逆検索の場合、コントロール全体が検索されますが、検索はドキュメントの下部から開始され、ドキュメントの先頭まで検索されます。

注意

パラメーターとして をstring受け取るメソッドではFind、 内の複数行のテキストに含まれるテキストをRichTextBox見つけることができません。 このような検索を実行すると、負の値 (-1) が返されます。

適用対象