Hi,
I have a RichTextBox, and there are about more than 1000 occurrences of a specified search string.
I use the following function to color all the occurrences:
public void ColorAll(string s)
{
rtbxContent.BeginUpdate();
int start = 0, current = 0;
RichTextBoxFinds options = RichTextBoxFinds.MatchCase;
start = rtbxContent.Find(s, start, options);
while (start >= 0)
{
rtbxContent.SelectionStart = start;
rtbxContent.SelectionLength = s.Length;
rtbxContent.SelectionColor = Color.Red;
rtbxContent.SelectionBackColor = Color.Yellow;
current = start + s.Length;
if (current < rtbxContent.TextLength)
start = rtbxContent.Find(s, current, options);
else
break;
}
rtbxContent.EndUpdate();
}
But I found it's very slow.
However, if I color all the occurrences of another word, which has less number of occurrences in the same text, I found it's very fast.
So I guess the slowness is from (these two line might get UI refresh involved):
rtbxContent.SelectionColor = Color.Red;
rtbxContent.SelectionBackColor = Color.Yellow;
Is there a faster way of doing the same job, such as, I do the coloring in the memory, and then I display the result at one-go?
Do I make myself clear?
Thanks.