Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFind a string within a TextBox
Keywordsfind string, find text, find, search, TextBox
CategoriesStrings, 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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated