I'm developing a richtextbox that recognises keywords (over 500) and colors them a certain color. This is done by selecting the start index and end index of the word and using this.SelectionColor = Color.Green;

This works well but performance is poor when the document gets large as it's literally going through each keyword every keypress, selecting the text and changing it. What I need is for the RTF code (\cf1 etc) to be replaced as this will improve performance.

The problem is how do I convert the index of the keyword from the richtextbox to the RTF file?

A keyword of 'customer' would be at index 0 on the rtb but not on the rtf as there would be other rtf header text ahead of it. Using rtf.Insert(startIndex, "\\cf2"); would put the color tag in the wrong place.

I could just search for keywords in the rtf file but this would cause problems firstly by being slower as the rtf will be larger and also it may index keywords that are in headers etc.

eg. If a keyword was 'new' it may treat the new in the header below as a keyword and insert the cf* tag.

{
\rtf1\ansi\ansicpg1252\deff0\deflang1033
{\fonttbl{\f0\fnil\fcharset0 Courier New;}}
}


Any help appreciated.