Search a String in A TextBox and Highlight in VB6

This tutorial provides to create a program that has the capability of finding a string in a textbox and highlight it using Visual Basic 6.0. Now, let's start this tutorial! 1. Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open. 2. Next, add only one Button named Command1 and labeled it as "Find a String". Add also one TextBox named TextBox1 for inputting any text on it. You must design your interface like this: design 3. Create a function named FindString with the parameters of Control, FindString, and StartPos As Integer = 1. Put this code below.
  1. Private Function FindString(Control As Control, FindStr As String, Optional StartPos As Integer = 1) As Boolean
  2. Dim a As Integer
  3. a = InStr(StartPos, LCase$(Control.Text), LCase$(FindStr))
  4. If a = 0 Then
  5. FindString = False
  6. Else
  7. FindString = True
  8. Control.SetFocus
  9. Control.SelStart = a - 1
  10. Control.SelLength = Len(FindStr)
  11. End If
  12. End Function
We have created this function named FindString to be able to find a specific text on the textbox. We have initialized variable a as Integer that will be equal to the InStr function of StartPos of 1, LCase or lower case function of the control which is the textbox, and LCase or lowercase function of FindStr variable that will be equal to the string that we need to search. If variable a is equal to zero then FindString = False means that the searched string is not found. Otherwise if found, the textbox will be focus and highlighted, starts at the first letter of the string, and also highlights the length of the string as we have the SelLength function of the control. 4. For button1 that has the function to search the desired text in the textbox, put this code below.
  1. Private Sub Command1_Click()
  2. Dim xx As String
  3. xx = InputBox("Enter a word to search:")
  4. If FindString(Text1, xx, 1) = True Then
  5. MsgBox ("Found text!")
  6. Else
  7. MsgBox ("Not found text!")
  8. End If
  9. End Sub
We have initialized first the variable xx as String that serves as the InputBox and the input which text is searched in the textbox. Then we have created an If Else statement. We used the FindString Function, Text1 as the control, xx as the FindStr, and 1 as the starting position of the text. If it is true, then the program will prompt as found and otherwise not found.

Output:

outputoutput Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer Mobile: 09102918563 Telephone: 826-9296 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Visit my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment