Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

Effective coloring in RichTextBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jakub Cermak - 28 Mar 2008 19:51 GMT
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?

Signature

Jakub Cermak

AMercer - 29 Mar 2008 01:30 GMT
> 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.
Jakub Cermak - 29 Mar 2008 21:27 GMT
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?
Signature

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,
[quoted text clipped - 19 lines]
> Maybe there is an alternative to RichTextBox, but I don't know what it is.  
> Sorry there is no good news in this post.
AMercer - 30 Mar 2008 21:27 GMT
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.