Search a string in a textbox
I developed a program that opens a text file containing customers. I added the program that has a button which allows to compare the string you search for my textbox which contains the text file
Here is my code that works:
Code:
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1. Click
Dim searchmot As String
Dim PosMot As Integer
searchmot = TextBox1. Text
PosMot = InStr (TextBox5.Text.ToLower, TextBox1.Text.ToLower)
If PosMot <> 0 Then
'PosMot from 1 to Len (Text1.Text), while SelStart ranges from 0 to Len (Text1.Text) -1
'We must therefore remove 1
TextBox5. SelectionStart = PosMot - 1 'set cursor position
TextBox5. SelectionLength = Len (searchmot) 'defined length highlight
TextBox5. Focus () "gives the focus to the box text1
Else
MsgBox ( "Unknown Client")
End If
End Sub
The problem is that if I take a string and then add a button that allows you to see if it exists.
I search my program highlights the first but impossible to know if my list contains another.
Can anyone please help me?
Re: Search a string in a textbox
Use a For Each loop:
Code:
For Each searchmot in TextBox5. Text. ToLower
...
Next
Re: Search a string in a textbox
Could you explain me in detail in my case hot to use "for each" loop and give me a little more information on the same?
Re: Search a string in a textbox
The for each loop may not be ideal for strings which are a series of characters ...
But you can do something like:
Code:
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1. Click
Dim TextBoxWitness As New TextBox
TextBoxWitness. Text = TextBox5. Text
Dim searchmot As String
Dim PosMot As Integer
Dim numIndex = 0
Dim counter = 0
searchmot = TextBox1. Text
PosMot = InStr (TextBoxWitness.Text.ToLower, TextBox1.Text.ToLower)
For Each character In TextBoxWitness.Text
If PosMot <> 0 Then
counter = 1
'PosMot from 1 to Len (Text1.Text), while SelStart ranges from 0 to Len (Text1.Text) -1
'We must therefore remove 1
numIndex = TextBoxWitness.Text.indexOf (searchmot)
TextBoxWitness. Text = TextBoxWitness.Text.Remove (numIndex, searchmot.Length)
PosMot = InStr (TextBoxWitness.Text.ToLower, searchmot.ToLower)
TextBox5.SelectionStart = PosMot 'set cursor position
TextBox5.SelectionLength = Len (searchmot) 'defined length highlight
TextBox5.Focus () "gives the focus to the box text1
Else
If counter = 0 Then
MsgBox ( "Unknown Client")
Exit For
End If
End If
Next
End Sub
Re: Search a string in a textbox
The easiest way would be to build the list of possible index for the word (you know the length of the word, only the index is sufficient). Something like
Code:
Dim indexes As List (Of Integer) = New List (Of Integer) ()
Dim string As String = "This is TechArena Website"
Dim search As String = "TechArena"
Dim pos As Integer = - 1
Do
pos = string. IndexOf (search, pos + 1)
If pos <> - 1 Then
indexes. Add (pos)
End If
End While
While pos <> - 1
Once you have your list index is simple. When you click on after you take the next index of the list and you changed the SelectionStart based, the length does not change. You can easily make a back button too.