CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Patrick Smacchia [MVP C#]


Some RichTextBox tricks

I have recently been responsible for refactoring the Code Query Language query editor in NDepend to fix some imperfections.

 

 

 

The CQL query editor implementation is based on a class derived from the System.Windows.Controls.RichTextBox class. It was the opportunity to learn some tricks that I would like to share in the current post.

 

Text Coloring

If you google how to color the text displayed in a RichTextBox, you’ll certainly end up using the coloring selection trick, using the RichTextBox method Select() and properties SelectionColor, SelectionBackColor:

 
Using a search engine is very misleading here. We end up to the conclusion that this approach comes with extremely bad performance, even on short text with just dozens of word to color.

 

A much better way we found is to use the Rtf / Rich Text Format capabilities of the RichTextBox. You just need to format a rtf string and use this code:

 

I won’t detail the Rtf format here. The Rtf string for the query above looks like:

 

Notice that using the SelectedRtf property is also a good way to prevent improper formatted text copy/pasted from Microsoft Word or a Browse for example.

 

Avoid flickering problem

When you update the content of your RichTextBox, you’ll certainly notice some pesky flickering. Hopefully, an efficient solution to this problem can be found here. Basically the solution consists in disabling text redrawing by calling some win32 APIs:

 

Testing for ScrollBars’ visibility

After looking for a way to test if the RichTextBox’s ScrollBars are visible or not, the only way I found is to infer this information from the delta between this.ClientRectangle and this.Size. This is certainly not the cleanest way but it is working well in every context I tried:

 

Get/Set the ScrollBars’ positions

To achieve this I came to the conclusion that it must be done throught the good-old win32. Being able to get and set the ScrollBars’ positions is especially useful to avoid some pesky automatic RichTextBox content re-locating I notice in some circumstances, such as inserting or modifying a long text. Here is the code:

 

Url Detection

Something that I wasn’t aware: if you want to display Urls that can be clicked in your text box, just set the RichTextBox.DetectUrls property to true. You can then use the RichTextBox.LinkClicked event to handle the url click.

 

 



Comments

Yann Trevin said:

Thanks for the tips. I have been already disapointed in the past by the limitations of the RichTextBox control support in winform, although the capabilities of the control itself are amazing And I am generally relunctant on using P/Invoke on non hardware or driver related stuff. But except by using a web container control, there is indeed not too many other choices. Thank you for sharing the info.

# July 7, 2008 3:00 PM

Reflective Perspective - Chris Alcock » The Morning Brew #131 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #131

# July 8, 2008 3:18 AM

2 Static » Blog Archive » Some RichTextBox tricks said:

Pingback from  2 Static  » Blog Archive   » Some RichTextBox tricks

# July 8, 2008 3:37 AM

Dew Drop - July 8, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 8, 2008 | Alvin Ashcraft's Morning Dew

# July 8, 2008 7:33 AM

make money online said:

Thanks for the great post! Looking forward to many more. B>)

# July 8, 2008 7:52 AM

Chris Pietschmann said:

VB.NET: Syntax Highlighting in a RichTextBox control

# July 8, 2008 6:50 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Patrick Smacchia

Patrick Smacchia is a Visual C# MVP involved in software development for over 15 years. After graduating in mathematics and computer science, he has worked on software in a variety of fields including stock exchange, airline ticket reservation system as well as a satellite base station at Alcatel. He's currently a software consultant and trainer on .NET technologies as well as the lead developer of the tool NDepend which provides numerous metrics and caveats on any compiled .NET application. He is the author of Practical .NET2 and C#2, a .NET book conceived from real world experience with 647 compilable code listings. Check out Devlicio.us!

Our Sponsors

Free Tech Publications