特定の検索オプションを適用し、 RichTextBox コントロール内の指定したテキスト範囲内で文字列を検索します。
Overloads Public Function Find( _
ByVal str As String, _ ByVal start As Integer, _ ByVal end As Integer, _ ByVal options As RichTextBoxFinds _) As Integer
[C#]
public int Find(stringstr,intstart,intend,RichTextBoxFindsoptions);
[C++]
public: int Find(String* str,intstart,intend,RichTextBoxFindsoptions);
[JScript]
public function Find(
str : String,start : int,end : int,options : RichTextBoxFinds) : int;
パラメータ
- str
コントロール内で検索するテキスト。 - start
コントロールのテキスト内で検索を開始する位置。 - end
コントロールのテキスト内で検索を終了する位置。この値は -1 または start パラメータの値以上であることが必要です。 - options
RichTextBoxFinds 値のビットごとの組み合わせ。
戻り値
コントロール内の検索文字列が見つかった位置。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | str パラメータが null 参照 (Visual Basic では Nothing) でした。 |
ArgumentException | start パラメータが 0 未満でした。 |
ArgumentException | end パラメータの値が start パラメータの値未満でした。 |
解説
Find メソッドは、 str パラメータに指定されたテキストを検索し、コントロール内の最初に見つかった検索文字列の位置を返します。プロパティが負の値を返す場合は、検索対象の文字列がコントロールの内容の中には見つからなかったことを示します。このメソッドを使用して、コントロールのユーザーが使用できる検索機能を作成できます。このメソッドを使用して、テキストを検索し、特定の書式に置換することもできます。たとえば、ユーザーがコントロールに日付を入力した場合、コントロールの SaveFile メソッドを使用する前に、 Find メソッドを使用してドキュメント内のすべての日付を検索し、見つかった日付を適切な書式に置換できます。
このバージョンの Find メソッドを使用すると、検索条件の拡張または絞り込みができるオプションを指定できます。検索語の大文字と小文字を区別するオプション、または語の一部ではなく全体を検索するオプションを指定できます。 options パラメータで RichTextBoxFinds.Reverse 列挙体を指定することにより、ドキュメントの上部から下部へという既定の方向ではなく、下部から上部という方向でテキストを検索できます。このバージョンの Find メソッドを使用すると、コントロールのテキスト内で検索を開始および終了する位置を指定して、テキストの検索範囲を絞り込むこともできます。この機能により、検索範囲をコントロール内のテキストの特定部分に限定できます。 end パラメータに -1 の値が割り当てられている場合、このメソッドは通常の検索の場合 RichTextBox 内のテキストの末尾まで検索します。逆方向の検索では、 end パラメータに -1 の値が割り当てられている場合、テキストの末尾 (下部) から start パラメータで定義された位置まで検索されることを意味します。start パラメータと end パラメータに同じ値が指定された場合は、コントロール全体が通常検索で検索されます。逆方向検索の場合はコントロール全体が検索されますが、ドキュメントの下部から始めて上部まで検索されます。
メモ 文字列をパラメータとして受け入れる Find メソッドでは、 RichTextBox 内の複数行に含まれるテキストは検索できません。このような検索を実行すると、値 -1 が返されます。
使用例
[Visual Basic, C#, C++] RichTextBox のテキストの一部を対象に、メソッドの searchText
パラメータに渡された検索文字列の最初のインスタンスを検索する例を次に示します。コントロール内のテキストの検索範囲は、メソッドの searchStart
パラメータと searchEnd
パラメータで指定されます。 RichTextBox 内で検索文字列が見つかると、見つかったテキストの最初の文字のインデックス位置が返され、見つかったテキストが強調表示されます。それ以外の場合は -1 が返されます。またこの例では、 Find メソッドの options パラメータを使用して、大文字と小文字を区別して検索が実行されるように指定しています。この例は、このメソッドが richTextBox1
という名前の RichTextBox コントロールを含む Form のクラス内にあることを前提にしています。検索文字列の最初のインスタンスが見つかったら、この例を使用して、他のインスタンスを検索できます。
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
[C#]
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;
}
[C++]
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;
}
[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 オーバーロードの一覧