VB查找关键字
做一个记事本工具,窗体上有RichTextBox1,需要实现查找功能,要在RichTextBox1中查找关键字,请问各位如何实现..........界面如下图...
做一个记事本工具,窗体上有RichTextBox1,需要实现查找功能,要在RichTextBox1中查找关键字,请问各位如何实现..........界面如下图
展开
4个回答
展开全部
Dim number As Long
Private Function FindStr(ByVal SLeft As Long, ByVal SFind As String, ByVal SSFind As String)
If Len(SFind) > Len(SSFind) - SLeft Then
FindStr = -1
Exit Function
Else
FindStr = InStr(SLeft + 1, SSFind, SFind) - 1
End If
End Function
Private Sub Command1_Click()
number = FindStr(0, Text1.Text, RichTextBox1.Text)
If number = -1 Then
MsgBox "没有找到"
Command2.Enabled = False
Else
RichTextBox1.SelStart = number
RichTextBox1.SelLength = Len(Text1.Text)
Command2.Enabled = True
End If
End Sub
Private Sub Command2_Click()
number = FindStr(number + Len(Text1.Text), Text1.Text, RichTextBox1.Text)
If number = -1 Then
MsgBox "查找完毕"
Command2.Enabled = False
Else
RichTextBox1.SelStart = number
RichTextBox1.SelLength = Len(Text1.Text)
Command2.Enabled = True
End If
End Sub
注意RichTextBox1.HideSelection要设为false
Private Function FindStr(ByVal SLeft As Long, ByVal SFind As String, ByVal SSFind As String)
If Len(SFind) > Len(SSFind) - SLeft Then
FindStr = -1
Exit Function
Else
FindStr = InStr(SLeft + 1, SSFind, SFind) - 1
End If
End Function
Private Sub Command1_Click()
number = FindStr(0, Text1.Text, RichTextBox1.Text)
If number = -1 Then
MsgBox "没有找到"
Command2.Enabled = False
Else
RichTextBox1.SelStart = number
RichTextBox1.SelLength = Len(Text1.Text)
Command2.Enabled = True
End If
End Sub
Private Sub Command2_Click()
number = FindStr(number + Len(Text1.Text), Text1.Text, RichTextBox1.Text)
If number = -1 Then
MsgBox "查找完毕"
Command2.Enabled = False
Else
RichTextBox1.SelStart = number
RichTextBox1.SelLength = Len(Text1.Text)
Command2.Enabled = True
End If
End Sub
注意RichTextBox1.HideSelection要设为false
展开全部
VB.NET:
下面这个函数返回每个要搜索的字符串的起始点
Private Function FindNumberOfOccurrences(ByVal Origin As String, ByVal Search As String) As String
Dim iPos As Integer
Dim strSearchFor As String = Search
Dim strSearchIn As String = Origin
Dim returnStr As String = ""
Do
iPos = strSearchIn.IndexOf(strSearchFor, iPos)
If iPos <> -1 Then
returnStr += iPos & vbCrLf
iPos += strSearchFor.Length
End If
Loop Until iPos = -1
Return returnStr
End Function
然后用split(chr13)分开之后,用一个循环分别select()就行
下面这个函数返回每个要搜索的字符串的起始点
Private Function FindNumberOfOccurrences(ByVal Origin As String, ByVal Search As String) As String
Dim iPos As Integer
Dim strSearchFor As String = Search
Dim strSearchIn As String = Origin
Dim returnStr As String = ""
Do
iPos = strSearchIn.IndexOf(strSearchFor, iPos)
If iPos <> -1 Then
returnStr += iPos & vbCrLf
iPos += strSearchFor.Length
End If
Loop Until iPos = -1
Return returnStr
End Function
然后用split(chr13)分开之后,用一个循环分别select()就行
参考资料: 个人看法,有意者电联
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以提供你个思路:先读取关键字字符串及其长度,再在richtextbox里截取该长度的字符串,比较,再把seltext设置成找到的字符串即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-02-03
展开全部
http://www.softhy.net/down.asp?id=8526&no=1
源码~测试过 你自己慢慢研究吧。
源码~测试过 你自己慢慢研究吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询