Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function help

Status
Not open for further replies.

Brenton

Technical User
Jul 29, 2002
43
US
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
 
Hi
There are two things about this function which confused me at first.
1. The return datatype is not declared. It is generally a good idea to do this, and if you have Option Strict on (also a good idea), this is required.

2. The return value is not actually used. The function could be made into a sub which works just as well. I've posted it below. I made a couple of other changes so that it works with option strict on.

The idea of the sub is that it looks for the text in the text box string. If it finds it, then it colors it and calls the sub again, starting at the position past the first occurence. It keeps doing this until all occurences are found.

Incidentally, this is called recursion and is fine for a small amount of text. If you've got more than say 100 occurences of the word in the text, it is not a good solution. This is because calls to the sub will keep building up for each occurence, which is a waste of memory. But if you find that it works without any noticeable delay, then just use it.

I would have done this using Regular Expressions to find all the matches and then for each match, set the color of the word in the box.

If you need any more help, let me know.

Mark

Private Sub 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(CInt(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 = CInt(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
Call FindIt(Box, Srch, sColor, Start)
End If
End Sub
 
And just to add another criticism....

Optional parameters don't work in C# so the code is better written using overloaded subs to take the parameters and a call to a further sub to do the actual work....

Private Sub FindIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.Color, ByVal Start As Long)

Call DoIt(Box, Srch, sColor,Start)

End Sub

Private Sub FindIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.Color)

Call DoIt(Box, Srch, sColor,Start, 1)

End Sub

Private Sub DoIt(ByRef Box As RichTextBox, ByVal Srch As String, ByVal sColor As System.Drawing.ColorPrivate ByVal Start As Long)

Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr

Source = Box.Text

retval = InStr(CInt(Start), Source, Srch, CompareMethod.Text)

If retval <> 0 Then
With Box
.SelectionStart = CInt(retval) - 1
.SelectionLength = Len(Srch)
.SelectionColor = sColor
End With
Start = retval + Len(Srch)
Call DoIt(Box, Srch, sColor, Start)
End If
End Sub

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top