DoubleBuffering a RichTextBox, how to paint
<p>I am looking at this website <a href="http://www.bobpowell.net/doublebuffer.htm" rel="nofollow">http://www.bobpowell.net/doublebuffer.htm</a> on double buffering. I like the idea of it and would like to use it on my RichTextBox class because I am trying to hold a few (10k+) lines in the box and every time I scroll or change a character, copy the viewable text to a string and place that on another rtb to be syntax colored. The problem is when I start to scroll I get really bad flicker in my rtb that holds all the text. I would like to wait until the user is done scrolling (if they scroll more than one page) to redraw the text in the rtb. Will double buffering help this? And how do I actually "paint" the text in the rtb as in this method from the website listed?</p>

<p>Method:</p>

<pre><code>protected override void OnPaint(PaintEventArgs e)
{
  if(_backBuffer==null)
  {
    _backBuffer=new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
  }
  Graphics g=Graphics.FromImage(_backBuffer);
  //Paint your graphics on g here
  g.Dispose();
  //Copy the back buffer to the screen
  e.Graphics.DrawImageUnscaled(_backBuffer,0,0);
  //base.OnPaint (e); //optional but not recommended
}
</code></pre>

<p>Edit:
You must also set the TextBox style to this</p>

<pre><code>this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
</code></pre>
