i use the below code to search and highlight the matching text, but it only shows the first instance of match (only 1 result), i would like the next matching text to get highlighted each time i click the button (like a 'find next' button)
i also need to get the line number of highlighted item. thank you.
'textbox1 is the textbox with content i need searched.
'textbox2 is where search string is typed.
Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer
Dim i As Integer
If endPosition < 1 Then
If Not matchCase Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, Me.Text.Length)
End If
Else
If matchCase = False Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, endPosition)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, endPosition)
End If
End If
If i > -1 Then
If highlightText Then
TextBox1.Focus()
TextBox1.SelectionStart = i
TextBox1.SelectionLength = textToFind.Length
End If
End If
Return i
End Function
' i would like the below button to be a "find next" button, also to show the line number in msgbox of highlighted item, like msgbox(linenumber)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SearchText(TextBox2.Text, 0, TextBox1.TextLength, True, False)
End Sub
i also need to get the line number of highlighted item. thank you.
'textbox1 is the textbox with content i need searched.
'textbox2 is where search string is typed.
Public Overloads Function SearchText(ByVal textToFind As String, Optional ByVal startPosition As Integer = 0, Optional ByVal endPosition As Integer = 0, Optional ByVal highlightText As Boolean = True, Optional ByVal matchCase As Boolean = False) As Integer
Dim i As Integer
If endPosition < 1 Then
If Not matchCase Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, Me.Text.Length)
End If
Else
If matchCase = False Then
textToFind = textToFind.ToLower
Dim temp As String = TextBox1.Text.ToLower
i = temp.IndexOf(textToFind, startPosition, endPosition)
Else
i = TextBox1.Text.IndexOf(textToFind, startPosition, endPosition)
End If
End If
If i > -1 Then
If highlightText Then
TextBox1.Focus()
TextBox1.SelectionStart = i
TextBox1.SelectionLength = textToFind.Length
End If
End If
Return i
End Function
' i would like the below button to be a "find next" button, also to show the line number in msgbox of highlighted item, like msgbox(linenumber)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SearchText(TextBox2.Text, 0, TextBox1.TextLength, True, False)
End Sub