Tools Links Login

Find and Highlight Substring

' Given an editable textbox named Text1, this code prompts to find a word and
' searches throught the textbox and highlights the first occurance of the
' found word (if exists).

Original Author: Bill Chelonis

Code

Private Sub FindFunction_Click()
Rem Find/highlight first occurance of a word in a textbox named Text1
Dim a As String
Dim y As Integer
a = InputBox("Find text: ", "Find", "")
Call Text1.SetFocus
SendKeys ("^{HOME}")
y = 1
Do Until y = Len(Text1.text)
Rem check if word was located
If Mid(UCase$(Text1.text), y, Len(a)) = UCase$(a) Then
   Rem highlight the found word and exit sub
   For x = 1 To Len(a)
    SendKeys ("+{RIGHT}")
   Next x
   Exit Do
End If
Rem do nothing if carriage return encountered else highlight found word
If Mid(Text1.text, y, 1) = Chr$(13) Then
Else
Rem move the cursor to the next element of text
SendKeys ("{RIGHT}")
End If
y = y + 1
If y > Len(Text1.text) Then Exit Do
Loop
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 92 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.