次の方法で共有


RichTextBox.Find メソッド (Char , Int32)

RichTextBox コントロールのテキスト内で、検索開始位置を指定して、文字リストに含まれる文字のうち最初に見つかる文字を検索します。

Overloads Public Function Find( _
   ByVal characterSet() As Char, _   ByVal start As Integer _) As Integer
[C#]
public int Find(char[] characterSet,intstart);
[C++]
public: int Find(__wchar_tcharacterSet __gc[],intstart);
[JScript]
public function Find(
   characterSet : Char[],start : int) : int;

パラメータ

  • characterSet
    検索対象の文字の配列。
  • start
    コントロールのテキスト内で検索を開始する位置。

戻り値

コントロール内の検索文字が見つかった位置。

解説

このバージョンの Find メソッドは、 characterSet パラメータに指定されている文字リストの文字を検索し、最初に見つかった文字の位置を返します。たとえば、文字 'Q' を含む文字の配列を渡したとします。コントロールに "The Quick Brown Fox" というテキストが格納されている場合、 Find メソッドは 4 の値を返します。検索では、大文字と小文字は別の値と見なされます。

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

このバージョンの Find メソッドを使用し、start パラメータに値を指定することによって、コントロールのテキスト内の指定した検索開始位置から文字セットを検索できます。値 0 は、コントロール内のドキュメントの先頭から検索を開始することを示します。このバージョンの Find メソッドを使用して、検索対象の文字が含まれていないことが既に判明しているテキストや検索対象として重要でないテキストを検索対象から外し、検索範囲を絞り込むことができます。

使用例

[Visual Basic, C#, C++] RichTextBox の内容の中から、メソッドの text パラメータに渡された文字を検索する例を次に示します。検索は RichTextBox 内の、 FindMyText メソッドの start パラメータで指定した位置から開始します。 RichTextBox 内でテキスト配列の内容が見つかった場合、メソッドはその値のインデックスを返します。それ以外の場合は -1 を返します。この例は、このメソッドが Form クラス内に配置されていることを前提にしています。このクラスには、 richTextBox1 という名前の RichTextBox 、およびこの例で定義されているクリック イベント処理メソッドに関連付けられている button1 という名前の Button コントロールが格納されています。

 
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

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

[C++] 
private:
    void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        Char temp0 __gc [] = {'B', 'r', 'a', 'v', 'o'};
        MessageBox::Show(FindMyText(temp0, 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;
    }

[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 オーバーロードの一覧