下面的例子根据在TextBox控件中输入的文本对RichTextBox控件进行搜索。如果找到指定的字符串,则显示一个消息框,指出包含指定单词的行号。要使用本例,在一个窗体上放置一个RichTextBox控件,一个CommandButton控件,一个TextBox控件。给RichTextBox控件加载一个文件,将下面的代码拷贝到窗体的Declarations段中。运行本例,在TextBox控件中输入文本,然后点击CommandButton控件。
Private Sub Command1_Click()
Dim FoundPos As Integer
Dim FoundLine As Integer
FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)
If FoundPos <> -1 Then
text.FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
MsgBox "Word found on line " & CStr(FoundLine)
Else
MsgBox "Word not found."
End If
End Sub