0

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);
    
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
AnotherUser
  • 1,311
  • 1
  • 14
  • 35
  • TextBoxes in WinForms do not use the Paint event. They are doing their own thing. – LarsTech Aug 17 '12 at 17:06
  • So then why does no text display when I use this in my rtb? Shouldnt, from your comment, the text be displayed no matter what I put in this overriden call. And yes, I have base.OnPaint(e) uncommented when I did this and still no text – AnotherUser Aug 17 '12 at 17:10
  • Can you type in your version of the RichTextBox? What happens? – LarsTech Aug 17 '12 at 17:16
  • When I type the cursor moves for each character, but the text is not there to be seen. – AnotherUser Aug 17 '12 at 17:26
  • Can't repro it with the posted code. I see the rich text. – LarsTech Aug 17 '12 at 17:35
  • What does that last comment mean....? – AnotherUser Aug 17 '12 at 17:41
  • I can't reproduce the problem you are seeing with the code you posted in the question. I see text in my control. – LarsTech Aug 17 '12 at 17:49
  • Yes, you told the control not to worry about painting and that you are taking care of it. That obviously isn't going to work. I don't think there is much you can do here except have less text in the control. You are at the mercy of windows here. You can try [this answer](http://stackoverflow.com/a/6550415/719186) in regards to minimizing some of the flickering. – LarsTech Aug 17 '12 at 18:14
  • Thank you Lars, I have looked at the page prior today. I decided to start a thread and lets the user view the text plain until the thread can color it... Best I think I can do – AnotherUser Aug 17 '12 at 18:27

0 Answers0