19

There are plenty of articles addressing flicker in Windows Forms. The majority recommend setting DoubleBuffered = true or setting a bunch of ControlStyle flags. However, none of these help reduce a TextBox flickering.

Here are a couple of related questions:

To reproduce the issue, create a new WinForms project, add a TextBox, enable multi-line, disable word-wrap, add a bunch of text, set Anchor to Left+Right+Top+Bottom. Now run and resize. The text flickers. For text boxes inside a couple of nested TableLayoutPanels the flicker on resize is even worse.

Applying the solutions proposed in the above questions at best do not fix the flicker; if I get experimental and set the protected ControlStyle on TextBox I can break it completely (by enabling UserPaint) but not eliminate the flicker.

So, is there any way at all to fix the flickering of the text in a TextBox?

Community
  • 1
  • 1
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • 1
    Interesting post, I have similar issues with a label control, but on the basis that I read DoubleBuffer should only be set with AllPaintInWmPaint which should only be set with UserPaint I didn't double buffer it... – Ian Aug 26 '09 at 09:35
  • @romkyns, I've updated my answer with a solution I've used in the past. – Ash Aug 26 '09 at 11:30

4 Answers4

16

I usually use a RichTextBox instead of a multiline TextBox. By setting the DetectUrls- and ShortcutsEnabled-properties to false the RTB behaves very similar to a TextBox and ... it's flicker-free.

miasbeck
  • 1,046
  • 9
  • 14
  • This is definitely the quickest and easiest solution. I took a custom control based on TextBox and just changed the parent to RichTextBox and - problem solved. – Andy Jun 15 '12 at 09:09
  • How was this not the accepted answer? It's completely accurate, thanks! (Granted, it wouldn't help flicker issues for child controls that weren't textboxes, but that wasn't the question.) – neminem Aug 13 '12 at 17:55
  • Too bad it's not supported for UWP :( – kayleeFrye_onDeck Jun 05 '19 at 03:54
5

In Windows Forms, the DoubleBuffered property does not affect child controls such as text boxes. Instead it affects just the Form or Panel it is set for.

If you want double-buffering for child elements on a form, you will need to implement manual double-buffering.

Bob Powell has written a good article (and others) on how to do this.

Also, from a forum answer Bob also says:

The ownership of a window means that they will flicker uncontrollably because you cannot double-buffer outside of the target windows area. A panel with child controls cannot be made to double buffer itself and it's children for example.

The only way to do this correctly is to create a single control that does all the drawing using a form of retained mode graphics system.

Therefore, to get flicker-free textbox resize using manual double-buffering you would need to somehow render the textbox to your back buffer and then display it as part of the buffered update. If even possible: I do not expect this would be easy.

[Update]

Some other answers have said this is a problem with Windows Forms specifically. This is not correct, it is actually deeper than that and is caused by Windows GDI. As an example, open Notepad/Wordpad etc. and paste a large chunk of text, resize the window, and notice the same flickering issue.

Here is a basic solution I used years ago to do something similar. It is a simple form containing a multiline textbox and a custom class inheriting from Panel. Both controls have the same location and size. It uses the Forms ResizeBegin and ResizeEnd to show the panel when resizing, and the textbox otherwise. It's not perfect but it does eliminate the flickering.

   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bm = null;

        private void textBox1_Resize(object sender, EventArgs e)
        {

            Graphics g = textBox1.CreateGraphics();

            if (g.VisibleClipBounds.IsEmpty == false)
            {
                bm = new Bitmap((int)g.VisibleClipBounds.Width, (int)g.VisibleClipBounds.Height);

                textBox1.DrawToBitmap(bm, new Rectangle(0, 0, (int)g.VisibleClipBounds.Width, (int)g.VisibleClipBounds.Height));

            }

            g.Dispose();


        }

        private void panelDB1_Paint(object sender, PaintEventArgs e)
        {
            if (bm != null)
            {
                e.Graphics.DrawImageUnscaled(bm, 0, 0,bm.Width,bm.Height );
            }
        }

        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            panelDB1.BringToFront();  
        }

        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            panelDB1.SendToBack();   
        }
}

class PanelDB : Panel
{
    public PanelDB()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer,true);       
        //this.DoubleBuffered = true; 
    
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
    }
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Ash
  • 60,973
  • 31
  • 151
  • 169
  • Thanks. The article might be useful to people unfamiliar with double buffering techniques, but doesn't really help address the question. Regarding window ownership - I am getting the feeling that the native TextBox on windows is inherently flickery and this is unfixable without extreme hacks... – Roman Starkov Aug 26 '09 at 10:15
  • I've added an update to my question. Unfortunately the answer to your question is simply "you can't", howver I thought some background info would be useful. – Ash Aug 26 '09 at 10:55
  • On checking some of my old code I see it's simple to render a textbox without flicker, using the DrawToBitmap() method onto an in- memory bitmap. But this would simply create an on-screen image of your text box that never flickers when resized but is utterly useless as it doesnt allow you to enter text. – Ash Aug 26 '09 at 10:58
  • Accepted for this insight: "As an example, open Notepad / Wordpad etc and paste a large chunk of text, resize the window and notice the same flickering issue." - indeed, the built-in text box appears to be inherently flickery. – Roman Starkov Aug 26 '09 at 14:21
0

We have encountered same kind of problem in past and it comes out to be use of excessive docking and table layout panels. I will suggest, if possible, try to re-construct the UI with minimal use of docking (as table layout panel also uses docking internally).

Ramesh Soni
  • 15,867
  • 28
  • 93
  • 113
  • I've also observed that the flicker gets worse as the number of layout-related controls increases. Unfortunately there's still flicker even with the most minimal layout necessary to achieve basic resizing - like in my example. – Roman Starkov Oct 16 '09 at 22:53
-3

FUNCTION LockWindow AS LONG CONTROL SEND ghDlg, %TEXT_UPPER,%WM_SETREDRAW,0,0 CLEARBuffers END FUNCTION

FUNCTION UnlockWindow AS LONG ClearBuffers CONTROL SEND ghDlg,%TEXT_UPPER,%WM_SETREDRAW,1,0 END FUNCTION