Question
Is it possible to speed up the colorizing of lines in a RichtextBox?
Situation
I have log files where the lines with errors must be shown colorized. For this I use the RichttextBox
control which has a very poor perfomance.
What I reached until now
I did already this to speed it up:
- Using Richedit50W instead of the default Richedit20
- Building a List of wanted line numbers before (by reading the log natively from disk first)
- Avoiding to use
RichTextbox50.Lines.Count()
in the loop
After that I need 11 seconds for colorizing 100 lines of 2000 lines instad of 40 seconds before.
Other Questions
There are several other questions like the mentioned Richedit50W about speeding RichtextBox up or colorizing. But no answer helps with speeding up colorizing.
My code
Can I do anything more to speed it up?
private void ColorizeLog(List<int> ErrorLines)
{
// The List "ErrorLines" is filled with the wanted line numbers
// and was build before filling the RichtextBow with the log data
// Counting the lines before the for..next is 50% faster than:
// for (int lineNo = 0; lineNo < this.richTextbox50_Log.Lines.Count(); lineNo++)
// in combination the other usage of "countLines" deeper in the code
int countLines = this.richTextbox50_Log.Lines.Count();
for (int lineNo = 0; lineNo < countLines; lineNo++)
{
if (ErrorLines.IndexOf(lineNo) >= 0)
{
int pos1 = this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo);
int pos2 = lineNo < countLines - 1 ?
this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo + 1) - 1 :
this.richTextbox50_Log.Text.Length;
this.richTextbox50_Log.Select(pos1, pos2 - pos1);
this.richTextbox50_Log.SelectionColor = Color.White;
this.richTextbox50_Log.SelectionBackColor = Color.Red;
}
}