Highlight multiple occurrences of a search string in textarea

Hi. I have the following code which bolds and highlights (changes text color) text from a search textfield, but it is only doing this for the first occurrence. After reading through the docs, I see IndexOf returns only the first occurrence of the search string, but how do I continue the search in the remaining textarea and highlight other occurrences? Thinking a For Next loop, but not sure how to set it up and where to place it. Thoughts?

[code]dim charCount as Integer = txtSearchText.Value.Length
dim phraseStart as Integer = txtWholeText.Value.IndexOf(txtSearchText.Value)

'selects and highlights the phrase
txtWholeText.SelectionStart = phraseStart
txtWholeText.SelectionLength = charCount

if txtWholeText.TextColor = &c000000 then
txtWholeText.SelectionTextColor = &cFF0000
txtWholeText.SelectionBold = True
end if

txtWholeText.SelLength = 0[/code]

IndexOf has a second parameter called Start. Set that from the starting index after the first occurrence.

To be pedantic, start is an optional first parameter.

Thanks for the tip! I threw this into a loop, and it does work in highlighting each of the search strings, but it is also highlighting the first few characters in the textarea. For example, I am searching a term that is 4 characters long, and while each of the search strings are highlighting, also are the first 4 characters of the text, which does not match the search string. Here is the updated code

[code]dim charCount as Integer = txtSearchText.Value.Length
dim phraseStart as Integer

for i as Integer = 1 to txtWholeText.Value.Length
phraseStart = txtWholeText.Value.IndexOf(i, txtSearchText.Value)

'selects and highlights the phrase
txtWholeText.SelectionStart = phraseStart
txtWholeText.SelectionLength = charCount

txtWholeText.SelectionTextColor = &cFF0000
txtWholeText.SelectionBold = True
next

txtWholeText.SelectionLength = 0[/code]

I understand setting i to 1 will begin the search at the beginning, but even with changing this to another number, it is still getting the first couple of characters. Here’s a screenshot

The loop goes too long.

The for loop is an inefficient way of doing this repeated search because each search is started one character down the line rather than AFTER the results of the previous search. But I suspect that it also creates an error in that after the last occurrence of pota, the search starts returning -1

phraseStart = txtWholeText.Value.IndexOf(i, txtSearchText.Value)

So then you are asking to highlight from phraseStart (-1) to a few characters beyond this. I suspect that this is why the first few characters are highlighted.

Perhaps something more along the lines:

[code]Var charCount As Integer = txtSearchText.Value.Length.Length
Var phraseStart As Integer

Var startLocation As Integer = 0

phraseStart = txtWholeText.Value.IndexOf(startLocation, txtSearchText.Value)

While phraseStart <> -1

'selects and highlights the phrase
txtWholeText.SelectionStart = phraseStart
txtWholeText.SelectionLength = charCount

txtWholeText.SelectionTextColor = &cFF0000
txtWholeText.SelectionBold = True

startLocation = phraseStart + charCount
phraseStart = txtWholeText.Value.IndexOf(startLocation, txtSearchText.Value)

Wend

txtWholeText.SelectionLength = 0[/code]

That’s it! Thank you Robert! Just had to take out a couple .Lengths in your code, but it works. And yes, I’m sure this is much more efficient than my For Next loop. Was just drawing a blank on how to best perform it knowing there had to be a loop in there

Thanks again