RichTextBox.Find 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在内容中 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[])
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
从字符列表中搜索控件的第 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。 该示例要求将此方法放置在一个类中,该类 Form 包含一个名为 RichTextBox 的 richTextBox1 控件, Button 以及一个名为,该 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”的字符数组。 如果控件包含文本“快速布朗福克斯”,该方法 Find 将返回四个值。 大写字符和小写字符在搜索中被视为不同的值。
如果该属性返回负值,则未在控件的内容中找到要搜索的字符。 可以使用此方法搜索控件中的一组字符。 此方法的 Find 此版本要求搜索控件中包含的整个文档以查找字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是控件中字符位置的从零开始的索引。 确定字符的位置时,方法会将空格视为字符。
适用于
Find(String)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在控件中 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 。 如果在搜索字符串中找到 RichTextBox搜索字符串,该方法将返回一个值 true 并突出显示搜索文本,否则返回 false。 该示例要求将此方法置于包含Form命名RichTextBox的richTextBox1类中。
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 文本,并返回控件中第一个字符的位置。 如果该属性返回负值,则未在控件的内容中找到要搜索的文本字符串。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在控件中输入了日期,则可以使用 Find 该方法搜索文档中的所有日期,并在使用 SaveFile 控件的方法之前用适当的格式替换它们。
注释
Find接受string作为参数的方法找不到包含在其中的多个文本行上的RichTextBox文本。 执行此类搜索将返回负值(-1)。
适用于
Find(Char[], Int32)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在特定起点搜索控件的文本 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。 搜索从方法参数RichTextBox指定start的位置FindMyText开始。 如果在其中找到 RichTextBox文本数组的内容,该方法将返回找到的值的索引;否则返回 -1。 该示例要求将此方法放置在一个Form类中,该类包含一个名为RichTextBox的richTextBox1控件,以及Button一个名为该控件的控件,该控件连接到button1Click示例中定义的事件处理程序。
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”的字符数组。 如果控件包含文本“快速布朗福克斯”,该方法 Find 将返回四个值。 大写字符和小写字符在搜索中被视为不同的值。
如果该属性返回负值,则未在控件的内容中找到要搜索的字符。 可以使用此方法搜索控件中的一组字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是控件中字符位置的从零开始的索引。 确定字符的位置时,方法会将空格视为字符。
使用此方法的 Find 此版本,可以通过为参数指定值 start ,从控件文本中的指定起始位置搜索字符集。 值为零表示搜索应从控件文档的开头开始。 可以使用此方法的 Find 此版本缩小搜索范围,以避免已知道的文本不包含要搜索的指定字符或搜索中不重要的字符。
适用于
Find(String, RichTextBoxFinds)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在控件中 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 。 如果在搜索字符串中找到 RichTextBox,该方法将返回一个值 true 并突出显示文本;否则返回 false。 该示例还指定搜索中的选项,以匹配指定搜索字符串的事例。 该示例要求将此方法置于包含Form命名RichTextBox的richTextBox1类中。
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 文本,并返回控件中第一个字符的位置。 如果该属性返回负值,则未在控件的内容中找到要搜索的文本字符串。 可以使用此方法创建可提供给控件用户的搜索功能。 还可以使用此方法搜索要替换为特定格式的文本。 例如,如果用户在控件中输入了日期,则可以使用 Find 该方法搜索文档中的所有日期,并在使用 SaveFile 控件的方法之前使用适当的格式替换它们。
使用此方法的 Find 此版本,可以指定可用于扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词,而不是部分单词。 通过在参数中RichTextBoxFinds.Reverse指定options枚举,可以搜索文档底部到顶部的文本,而不是默认的从上到下搜索方法。
注释
Find接受string作为参数的方法找不到包含在其中的多个文本行上的RichTextBox文本。 执行此类搜索将返回负值(-1)。
适用于
Find(Char[], Int32, Int32)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在控件中 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”的字符数组。 如果控件包含文本“快速布朗福克斯”,该方法 Find 将返回四个值。 大写字符和小写字符在搜索中被视为不同的值。
如果该属性返回负值,则未在控件的内容中找到要搜索的字符。 可以使用此方法搜索控件中的一组字符。 如果找到方法 characterSet 参数中提供的字符列表中的字符,则此方法返回的值是基于控件中字符位置的从零开始的索引。 确定字符的位置时,方法会将空格视为字符。
使用此方法的 Find 此版本,可以通过为控件中的一系列文本指定值 start 和 end 参数来搜索字符集。 参数的值为零 start ,指示搜索应从控件文档的开头开始。 参数的 -1 值 end 指示搜索应在控件中的文本末尾结束。 可以使用此版本的 Find 方法将搜索范围缩小到控件中的特定文本范围,以避免搜索对应用程序需求不重要的文档区域。
适用于
Find(String, Int32, RichTextBoxFinds)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在控件中的文本中搜索控件中 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 。 搜索起始位置由方法的 start 参数指定。 如果在搜索字符串中找到 RichTextBox,该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本;否则,它将返回值 -1。 该示例还指定搜索中的选项,以匹配指定搜索字符串的事例。 该示例要求将此方法置于包含Form命名RichTextBox的richTextBox1类中。 可以使用此示例执行“查找下一步”类型的操作。 找到搜索文本的实例后,可以通过更改参数的值 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 此版本,可以指定可用于扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词,而不是部分单词。 通过在参数中RichTextBoxFinds.Reverse指定options枚举,可以搜索文档底部到顶部的文本,而不是默认的从上到下搜索方法。 此方法的 Find 此版本还可以通过选择控件文本中的特定起始位置来缩小对文本的搜索范围。 借助此功能,可以避免可能已搜索的文本,或者所搜索的特定文本已知不存在。
RichTextBoxFinds.Reverse在参数中options指定值时,参数的值start指示反向搜索将结束的位置,因为在使用此版本的Find方法时,搜索将从文档底部开始。
注释
Find接受string作为参数的方法找不到包含在其中的多个文本行上的RichTextBox文本。 执行此类搜索将返回负值(-1)。
适用于
Find(String, Int32, Int32, RichTextBoxFinds)
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
- Source:
- RichTextBox.cs
在控件中的文本中搜索控件中的 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.
示例
下面的代码示例在搜索传入方法参数的搜索字符串RichTextBox的第一个实例中searchText搜索文本部分。 在控件中搜索文本的范围由searchStartsearchEnd方法的参数指定。 如果在搜索字符串中找到 RichTextBox,该方法将返回找到的文本的第一个字符的索引位置,并突出显示找到的文本;否则,它将返回值 -1。 该示例还使用 options 该方法的参数 Find 来指定找到的文本应与搜索字符串的事例匹配。 该示例要求将此方法置于包含
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 此版本,可以指定可用于扩展或缩小搜索范围的选项。 可以指定选项,使你能够匹配搜索词的大小写或搜索整个单词,而不是部分单词。 通过在参数中RichTextBoxFinds.Reverse指定options枚举,可以搜索文档底部到顶部的文本,而不是默认的从上到下搜索方法。 此方法的 Find 此版本还可以通过选择控件文本中的特定开始和结束位置来缩小搜索文本的范围。 此功能使你可以将搜索范围限制为控件文本的特定部分。 如果将负值 (-1) 赋给 end 参数,则该方法将搜索到普通搜索中 RichTextBox 文本的末尾。 对于反向搜索,分配给 end 参数的负值一(-1)表示文本将从文本末尾(底部)搜索到参数 start 定义的位置。
start提供参数时end,将搜索整个控件的正常搜索值。 对于反向搜索,将搜索整个控件,但搜索从文档底部开始,搜索到文档顶部。
注释
Find接受string作为参数的方法找不到包含在其中的多个文本行上的RichTextBox文本。 执行此类搜索将返回负值(-1)。