|
-
July 25th, 2025, 04:55 AM
#1
Richtextbox flicker when resizing
Hallo, I have a form that has a Richtextbox control.
Every control is generate with cp.ExStyle |= 0x02000000; to reduce flickering.
And the richtextbox has
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,
true);
base.DoubleBuffered = true;
But when I maximize the form, the new part of the richtextbox is for some seconds, black.
Could you suggest me how to avoid this effect?
Thank you.
Jessy.
-
July 27th, 2025, 02:54 PM
#2
Re: Richtextbox flicker when resizing
https://www.codeproject.com/Articles...-Double-Buffer
Tip (by Tim McCurdy): SetStyle(ControlStyles.ResizeRedraw, true);
Setting this flag causes the control to repaint itself when resized.
But note that this page is 20 years old.
https://learn.microsoft.com/en-us/do...s-and-controls
TBH, perhaps strip out all the attempts to "optimise" the behaviour and see how the default behaves, especially when MS themselves say
For most applications, the default double buffering provided by the .NET Framework will provide the best results
-
August 18th, 2025, 02:55 AM
#3
Re: Richtextbox flicker when resizing
This happens because the RichTextBox control is a wrapper around the native Windows control (Riched20.dll), which doesn?t always play nicely with .NET?s DoubleBuffered or custom paint optimizations. That?s why even if you set ControlStyles.OptimizedDoubleBuffer, the flicker (or black background during maximize/resize) can still appear.
A few things you can try:
- Override background painting:
Sometimes forcing the control to repaint itself helps. You can subclass the RichTextBox and override OnPaintBackground to reduce the black flash.
HTML Code:
protected override void OnPaintBackground(PaintEventArgs e)
{
// Prevents default background paint
// Fill it manually if needed
e.Graphics.Clear(this.BackColor);
}
- Suspend/resume layout during resize:
In your form?s ResizeBegin and ResizeEnd events, you can call SuspendLayout() and ResumeLayout() to stop partial redraws while the form is maximizing.
- Consider WPF RichTextBox (if possible):
If you?re doing heavy UI work, hosting a WPF RichTextBox in a WinForms app via ElementHost gives smoother resizing because WPF handles double buffering more natively.
- Fallback option ? WM_SETREDRAW:
Some developers temporarily disable redraw messages during resize:
HTML Code:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, Int32 msg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 0x0b;
SendMessage(richTextBox1.Handle, WM_SETREDRAW, false, 0);
// do resize
SendMessage(richTextBox1.Handle, WM_SETREDRAW, true, 0);
richTextBox1.Invalidate();
This stops the black rectangle issue in most cases.
👉 The short version: you?re running into a limitation of the native control, not just .NET buffering. The smoothest fixes tend to come from either suspending redraw during resize, or switching to a WPF RichTextBox if flicker-free UI is critical.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|