I am looking at this article https://web.archive.org/web/20121123031717/http://www.bobpowell.net/doublebuffer.htm 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?
Method:
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
}
Edit: You must also set the TextBox style to this
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);