VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Find and Highlight Substring

by Bill Chelonis (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

' 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).

Rate Find and Highlight Substring

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

Download this snippet    Add to My Saved Code

Find and Highlight Substring Comments

No comments have been posted about Find and Highlight Substring. Why not be the first to post a comment about Find and Highlight Substring.

Post your comment

Subject:
Message:
0/1000 characters