RichTextBox and color

L

Luc Bolly

Hi all.

It's not new, but I cannot find a really good solution. The context: I grab
text lines from log files in a RichTextBox and I would like to mark with a
specific color some keyworks (e.g. the errors). I wrote a function (see
hereunder) that works correctly when the number of lines is small (a few
tenth), but this goes really really slow when the number of lines containing
the keywords grows (lets say 10.000 lines). Here is the code:

private void Colorify(System.Windows.Forms.RichTextBox oRTBox, string
sValue, System.Drawing.Color oColor) {

oRTBox.SuspendLayout();

int i = oRTBox.Find(sValue, 0, RichTextBoxFinds.NoHighlight |
RichTextBoxFinds.MatchCase);

int iValue = sValue.Length;

while( i >= 0 && bStopAction==false) {

Application.DoEvents();

oRTBox.Select(i,iValue);

oRTBox.SelectionColor = oColor;

oRTBox.SelectionFont = new Font(oRTBox.SelectionFont, FontStyle.Bold);

i = oRTBox.Find(sValue, i+iValue, RichTextBoxFinds.NoHighlight |
RichTextBoxFinds.MatchCase);

}

oRTBox.Select(0,0);

oRTBox.ResumeLayout();

}

If anyone has an idea to optimize the processing, I would hear it with a
great pleasure. Maybe using something else that the RichTextBox ...
Kind regards,
Luc
 
G

Guest

Hi Luc,

One thing that might be slowing things down is the repetitive call to
Application.DoEvents, but I would suggest another approach

I think it would be a better idea for you to generate the RTF text
separately from the RTF control, and then assign the result to the RTF
property of the control.

You could do the same type of search and replace using Regular Expressions,
with just a little more knowledge of RTF.

Rick Hodder
 
L

Luc Bolly

Hi Rick.

Thanks for replying. My first version was without Application.DoEvents() and
was also very very slow. I added this call only to have a button to cancel
the operation when it takes too long.

Speaking about RTF text, I must say that I'm a beginner. I used it only
because I gave me the opportunity to specify colors in a text box (instead
of the TextBox). Could you give me some clues about how to handle RTF text
in a variable of type ? RTF ?. Your idea of using RegEx may also be used.
it is just that I always forget it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top