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?

Edited 3 Years Ago by deceptikon: Moved to C#

Your best bet is to modify the raw RTF, add your colour to the colour table header and then at the word you want to highlight, change the colour to the required index.

That way when your textbox automatically redraws itslef, it will already have the raw colour information and wont need you to stop and start paints. This will also remove the selection blue highlight you mentioned.

As a point of note, you may want to pause or buffer user input whilst you modify the colour information to prevent accidental typing to the incorrect location.

You can find the RTF spec Here

Edited 3 Years Ago by Ketsuekiame: Added RTF spec link

This question has already been answered. Start a new discussion instead.