coloring part of a text

I have a textarea containing endofline delimited text, and I would like to color certain words. The coloring process goes pretty quick untill the text contains one thousand lines, but it takes too long (even 8 seconds) when there are several thousand lines. Each line has a min of 4 words and a max of 20 words.
The code I’m using is shown below. I tried using a memoryblock but I’m not sure if I used it correctly: after initializing it (now it is commented), I replaced the only instance of “source” with “m”; but as I said, I dont know if that is the right procedure. I say this, because uncommenting the memoryblock line, some part of the text gets colored in the wrong place.
Anyway, is there any better way to accomplish what I’m trying to do? Thanks.

Protected Sub setContextClr()
#pragma DisableBackgroundTasks
#pragma DisableBoundsChecking
//#Pragma DisabledTextColor

dim start,i as Integer
TextArea1.selTextColor = kclrBlack
dim source as String = TextArea1.text
//dim m as MemoryBlock = source
start = 1
dim s as String = findField.text
if s <> “” then
dim substrLength as integer = Len(s)
start= instr(start,source, s)
If start < 1 then
end if

  TextArea1.SelStart = start-1
  TextArea1.SelLength = substrLength
  TextArea1.SelTextColor = findClr
  TextArea1.SelLength = 0
  start = start + substrLength

end if
End Sub

Look at “CUSTOM EDIT FIELD” a control written by Alex Restrepo a few years back, and currently maintained by someone (sorry I forgot who!) on this forum.

The trick is to only color what is visible, the CEF does NOT use a TextArea subclass, but is 100% canvas based, and works very well

Otherwise, put the coloring code in a Timer. Each iteration of the Timer can run for up to 3 ticks, then exit. The next iteration will pick up where the last left off. The UI will stay responsive and the text will colorize in waves.

I use this with great effect in RegExRX.

what about trying StyleRun ?
(if possible, I do not checked)

Combining both Dave and Kem’s techniques is probably the best of both worlds.

You could run the code for only the visible lines first, so to the user it is instant (use ScrollPosition to get the top visible line), and then use the timer to update the entire text. Maybe starting at the last line of the screen up to the end, since one can assume the reader will go down the text, then from the top until the top visible line.

Thanks for the advices. In fact I had thought of colorising the portion of text visible, but I found Kem’s solution excellent for my needs.