RichTextBox コントロールのテキスト内で、文字リストに含まれる文字のうち、最初に見つかる文字を検索します。
Overloads Public Function Find( _
ByVal characterSet() As Char _) As Integer
[C#]
public int Find(char[] characterSet);
[C++]
public: int Find(__wchar_tcharacterSet __gc[]);
[JScript]
public function Find(
characterSet : Char[]) : int;
パラメータ
- characterSet
検索対象の文字の配列。
戻り値
検索対象の文字が見つかったコントロール内の位置、または検索対象の文字が見つからない場合、あるいは char パラメータで空の検索文字が指定された場合は -1。
解説
このバージョンの Find メソッドは、 characterSet パラメータに指定されている文字リストの文字を検索し、最初に見つかった文字の位置を返します。たとえば、文字 'Q' を含む文字の配列を渡したとします。コントロールに "The Quick Brown Fox" というテキストが格納されている場合、 Find メソッドは 4 の値を返します。検索では、大文字と小文字は別の値と見なされます。
プロパティが負の値を返す場合は、検索対象の文字がコントロールの内容の中には見つからなかったことを示します。このメソッドを使用して、コントロール内で文字のグループを検索できます。このバージョンの Find メソッドは、コントロールに含まれているドキュメント全体が、文字の検索範囲であると想定します。メソッドの characterSet パラメータに指定されている文字リストの文字が見つかった場合、このメソッドは、その文字のコントロール内での位置を 0 から始まるインデックスで返します。このメソッドが文字の位置を判断するときは、空白も文字であると見なされます。
使用例
[Visual Basic, C#, C++] RichTextBox の内容の中から、メソッドの text
パラメータに渡された文字を検索する例を次に示します。 RichTextBox 内で text
配列の内容が見つかった場合、メソッドはその値のインデックスを返します。それ以外の場合は -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}).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
[C#]
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;
}
[C++]
private:
void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
Char temp1 [] = {'D', 'e', 'l', 't', 'a'};
MessageBox::Show(FindMyText(temp1).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;
}
[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 オーバーロードの一覧