另外一种方法,在窗体上添加一个listbox1和一个combox1控件,将搜索到的数据添加到combox1中。
private sub findtext(byval lhp as listbox)
dim i as integer
dim j as integer
dim m as integer
m=lhp.listcount
for i=0 to m-1
lhp.listindex=i
dim s as string
s=lhp.text
if instr(s,1,"abc")<>0 then
me.combox1.additem s
end if
next
end sub
你所说的就是类似于“搜索”,“搜索下一个”,下面给你一段代码,自己仔细研究去吧:
Private Sub serch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles serch.Click
yesornonext = False
findtext(1)
End Sub
'查找下一个
Private Sub serchnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles serchnext.Click
yesornonext = True
findtext(targetposition + 1)
End Sub
Private Sub findtext(ByVal startposition As Integer)
If yesornonext = False Then
findtextstr = Trim(InputBox("请输入要查找的文本:", "查找"))
End If
Dim activechild As Form = Me.ActiveMdiChild
If Not activechild Is Nothing Then
If Not activechild.ActiveControl Is Nothing Then
Dim therichtextbox As RichTextBox
therichtextbox = CType(activechild.ActiveControl, RichTextBox)
Dim pos As Integer
pos = InStr(startposition, therichtextbox.Text, findtextstr)
If pos > 0 Then
textnum = textnum + 1
targetposition = pos
therichtextbox.SelectionStart = targetposition - 1
therichtextbox.SelectionLength = Len(findtextstr)
Else
If yesornonext = False Then
MessageBox.Show("你要查找的文本不存在!", "提示", MessageBoxButtons.OK)
Else
MessageBox.Show("文本查找完毕!" & " " & "共" & CStr(textnum) & "处", "提示", MessageBoxButtons.OK)
End If
End If
End If
End If
End Sub
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_FINDSTRING = &H18F
Private Sub Form_Load()
List1.Clear
List1.AddItem "Apples"
List1.AddItem "Banana"
List1.AddItem "Bread"
List1.AddItem "Break"
Text1.Text = ""
End Sub
Private Sub Text1_Change()
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
End Sub