查找字符串问题

来源:百度知道 编辑:UC知道 时间:2024/07/02 02:34:27
比如,在记事本中,有个"查找"的选项,输入字符串,就能高亮显示它,找不到就输出错误信息.编写find函数,查找字符串(不要读写文件!)
要求写出函数体!
二楼的,你做得很好!我试过了,多次单击command1,结果仍是第一个(即某字符串多次出现,"查找"只能找出第一次出现的位置),能否改良?

Private Sub Command1_Click()
Dim a%
a = InStr(Text1, Text2)
If a = 0 Then
MsgBox "Havn't found them!"
Else
Text1.SelStart = a - 1
Text1.SelLength = Len(Text2)
Text1.SetFocus
End If
end sub

------------
你要求的改良
Private Sub Command1_Click()
a = InStr(Mid(Text1, Text1.SelStart + Text1.SelLength + 1), Text2)
If a = 0 Then
MsgBox "Havn't found them!"
Exit Sub
End If
Text1.SelStart = Text1.SelStart + a - 1 + Text1.SelLength
Text1.SelLength = Len(Text2)
Text1.SetFocus
End Sub
Private Sub Text2_Chang()
Text1.SelStart=0
Text1.SelLength=0
End Sub

用Instr查找,找到了就设置SelStart和SelLength