Hide focused richtextbox blue selection

Jan 10 2013 12:48 PM
I am doing a syntax highlighter (which is my first c# project) and I have some performance problems.

Every time I have to color a word, I stop the repaint, color the word, enable repaint and invalidate the richtextbox.

This is the richtextbox class:

namespace test
{
class rtb:RichTextBox
{
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int WM_SETREDRAW = 0x0b;

public void BeginUpdate()
{
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}
public void EndUpdate()
{
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
this.Invalidate();
}
}
}

The invalidation (refresh) causes, sometimes, some rows of text to flicker, which is unpleasant. If I don't stop the repaint and color the words directly, the blue selection of the word is visible.

I color the words with this method :

private void doPaint(int start, int length, Color selColor)
{
rtb1.BeginUpdate(); // rtb1 is the richtextbox (from rtb class)
rtb1.SelectionStart = start;
rtb1.SelectionLength = length;
rtb1.SelectionColor = selColor;
rtb1.SelectionLength = 0;
rtb1.EndUpdate();
}

How can I hide that blue selection, while the user is typing, and color the word? If that is not possible, how can I stop that small flicker caused by the refreshing of the richtextbox?

Thank you in advance !