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.
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.
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:
detail the Rtf format here. The Rtf string for the query above looks like:
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
Testing for ScrollBars’
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’
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:
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.