|
|
Title | Find a string within a TextBox |
Keywords | find string, find text, find, search, TextBox |
Categories | Strings, Controls |
|
|
Use the InStr Function.
|
|
Private TargetPosition As Integer
Private Sub FindText(ByVal start_at As Integer)
Dim pos As Integer
Dim target As String
target = txtTarget.Text
pos = InStr(start_at, txtBody.Text, target)
If pos > 0 Then
' We found it.
TargetPosition = pos
txtBody.SelStart = TargetPosition - 1
txtBody.SelLength = Len(target)
txtBody.SetFocus
Else
' We did not find it.
MsgBox "Not found."
txtBody.SetFocus
End If
End Sub
' Find the text.
Private Sub cmdFind_Click()
FindText 1
End Sub
' Find the next occurrance of the text.
Private Sub cmdFindNext_Click()
FindText TargetPosition + 1
End Sub
|
|
Formatted by
Neil Crosby
|
|
|
|
|
|