Close this search box.

Extending RichTextBox

For a while now I’ve been toying with the idea of writing a C# editor (almost an IDE, but with only the features that I need). Clearly the hardest part of this task would be getting some form of edit control working.. the obvious choice for which is the RichTextBox. Anyone who has tried using RichTextBox in this situation will know that it has a number of flaws, the most serious being that it’s rather slow when trying to colourise the text.

I’ve long since given up on using RichTextBox for a code editor, but while I was investigating its use I came across a number of ways to improve it’s functionality. I’ll present these here in a series of posts on the subject.. first up is how to speed up the control’s updating, as well as adding one little extra formatting option.

Faster Updating

There are articles on this subject on the internet. The standard method seems to be to send a WM_SETREDRAW message to prevent the control from being redrawn while updating. I had this implemented and running nicely when I came across another article showing that an extra speed increase can be gained by sending EM_SETEVENTMASK to prevent the control from handling events. Here’s the code:

+ Code to help speed up RichTextBox updating.

Although I’m using HandleRef here, I have absolutely no idea if this is the correct usage (or even if it’s needed). Full code will be at the end of this posting. I have not fully tested these methods so I have no idea what the performance gains might be (if there are any). If anyone wants to try benchmarking, please let me know the results.


It always bothered me that the SelectionAlignment property of RichTextBox does not allow fully justified text. Especially since I know it’s possible with the richedit control that RichTextBox wraps. Here’s some code that will replace SelectionAlignment with something a little nicer for those that are writing word processors in C#:

+ Code to allow justified text in the RichTextBox.

The TextAlign enumeration contains LeftRightCenter and Justify. Again, this is mostly untested — it should work on any machine with the richedit 2.0 common control (I believe), but I offer no guarantees.


Here’s the full code for the AdvRichTextBox control (plus the TextAlign enumeration):

+ Full AdvRichTextBox code.


I’ll add to this code in future posts and if anyone has any comments or suggestions (or even requests), feel free to ask them. Hope you found this useful.

posted on Tuesday, October 14, 2003 5:02 PM

This article is part of the GWB Archives. Original Author: Pete’s Weblog

Related Posts