I need help with this function. I am trying to find certain string and change the forcolor. I was given this function to try, and i need someone to explain it better.
Private Function FindIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.Color, Optional ByVal Start As Long = 0)
'Found at: 'Orginally written for VB6
Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr
Source = Box.Text 'put the text to search into the variable
If Start = 0 Then Start = 1 'the initial call doesn't pass a value
'for Start, so it will equal 0
retval = InStr(Start, Source, Srch, CompareMethod.Text) 'do the first search,
'starting at the beginning
'of the text
If retval <> 0 Then 'there is at least one more occurrence of
'the string
'the RichTextBox doesn't support multiple active selections, so
'this section marks the occurrences of the search string by
'making them Bold and Red
With Box
.SelectionStart = retval - 1
.SelectionLength = Len(Srch)
.SelectionColor = sColor
End With
Start = retval + Len(Srch) 'move the starting point past the
'first occurrence
'FindIt calls itself with new arguments
'this is what makes it Recursive
FindIt = 1 + FindIt(Box, Srch, sColor, Start)
End If
End Function
Thanks
Brenton
Private Function FindIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.Color, Optional ByVal Start As Long = 0)
'Found at: 'Orginally written for VB6
Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr
Source = Box.Text 'put the text to search into the variable
If Start = 0 Then Start = 1 'the initial call doesn't pass a value
'for Start, so it will equal 0
retval = InStr(Start, Source, Srch, CompareMethod.Text) 'do the first search,
'starting at the beginning
'of the text
If retval <> 0 Then 'there is at least one more occurrence of
'the string
'the RichTextBox doesn't support multiple active selections, so
'this section marks the occurrences of the search string by
'making them Bold and Red
With Box
.SelectionStart = retval - 1
.SelectionLength = Len(Srch)
.SelectionColor = sColor
End With
Start = retval + Len(Srch) 'move the starting point past the
'first occurrence
'FindIt calls itself with new arguments
'this is what makes it Recursive
FindIt = 1 + FindIt(Box, Srch, sColor, Start)
End If
End Function
Thanks
Brenton