Effective coloring in RichTextBox

J

Jakub Cermak

Hello, I've RichTextBox with quite long text (thousands of lines) and I'm
doing something line syntax coloring. I set different colors to each word,
number etc. with selecting the text (Select()) and the setting
SelectionColor. But it's very slow. I even tried to send the messages
manually and avoid the calling of SelectionColor (that's the slowest thing)
but it's not much better. How to do this effectively?
 
A

AMercer

Hello, I've RichTextBox with quite long text (thousands of lines) and I'm
doing something line syntax coloring. I set different colors to each word,
number etc. with selecting the text (Select()) and the setting
SelectionColor. But it's very slow. I even tried to send the messages
manually and avoid the calling of SelectionColor (that's the slowest thing)
but it's not much better. How to do this effectively?

I don't think there is a way to get good performance with the RichTextBox
methods. I think the reason is that every highlight operation you do results
in reconstituting the RichTextBox's .rtf field which is a very large string
if you have thousands of lines. Selecting text can also be costly if you are
using SelectionColor and/or SelectionBackColor.

To get good performance with a RichTextBox, you could build the rtb's .rtf
field (using stringbuilder) in one pass over the rtb's text looking for
things to highlight. The prerequisite is coming to terms with the MS RTF
specs and how they are used in a RichTextBox. Or you could try (trial and
error) the win32 apis and messages like EM_SETCHARFORMAT. Or maybe you could
use InkEdit which is supposed to be a superset of RichTextBox. All sound
daunting to me.

Maybe there is an alternative to RichTextBox, but I don't know what it is.
Sorry there is no good news in this post.
 
J

Jakub Cermak

Making RTF code by hand appeared on my mind, but that's what I didnt want to
do.
InkEdit isn'a solution. Quote from msdn library:
This control is a superset of the RichTextBox control. It extends the
RichTextBox control with the ability to capture, recognize, and display ink.
The selection is quite fast, i commented the coloring line, and it all took
3s, which is good. I played with the messages, but as I wrote it made it
faster only a little bit more.

Any more ideas?
 
A

AMercer

Any more ideas?

I don't much care for my earlier suggestions, but since you ask...

You could put a transparent window on top of your rtb window. Put your text
in the (scrolling) rtb window and put highlights (only those that are
visible) in the (nonscrolling) overlay. If your rtb is a simple single-font
spray of text, the calculations for highlighting the overlay should not be
too onerous.

A better approach might be rtb with transparent background on top and a
highlight underlay with opaque background underneath. User key/mouse actions
could be handled by the rtb in the usual way.

I'm not particularly fond of this suggestion either. MS has solved this
problem, to wit their handling of highlighting in visual studio. I wish they
would tell us how.
 

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