次の方法で共有


RichTextBox.Find メソッド (String)

RichTextBox コントロール内のテキストで文字列を検索します。

Overloads Public Function Find( _
   ByVal str As String _) As Integer
[C#]
public int Find(stringstr);
[C++]
public: int Find(String* str);
[JScript]
public function Find(
   str : String) : int;

パラメータ

  • str
    コントロール内で検索するテキスト。

戻り値

検索文字列が見つかったコントロール内の位置、または検索文字列が見つからない場合、あるいは str パラメータで空の検索文字列が指定された場合は -1。

解説

Find メソッドは、 str パラメータで指定されたテキストを検索し、コントロール内で最初に見つかった文字の位置を返します。プロパティが負の値を返す場合は、検索対象の文字列がコントロールの内容の中には見つからなかったことを示します。このメソッドを使用して、コントロールのユーザーが使用できる検索機能を作成できます。このメソッドを使用して、テキストを検索し、特定の書式に置換することもできます。たとえば、ユーザーがコントロールに日付を入力した場合、コントロールの SaveFile メソッドを使用する前に、 Find メソッドを使用してドキュメント内のすべての日付を検索し、見つかった日付を適切な書式に置換できます。

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

使用例

[Visual Basic, C#, C++] RichTextBox の内容全体で文字列を検索し、最初に見つかった文字列をメソッドのテキスト パラメータに渡す例を次に示します。 RichTextBox で検索文字列が見つかった場合、メソッドは値 true を返し、見つかった文字列を強調表示します。それ以外の場合は false を返します。この例は、このメソッドが richTextBox1 という名前の RichTextBox を保持する Form のクラス内にあることを前提にしています。

 
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


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


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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | RichTextBox.Find オーバーロードの一覧