<p>How can I set the protected <code>DoubleBuffered</code> property of the controls on a form that are suffering from flicker?</p>

[{'answer_id': 77023, 'author': 'Arno', 'author_id': 13685, 'author_profile': 'https://Stackoverflow.com/users/13685', 'pm_score': 4, 'selected': False, 'text': '<pre><code>public void EnableDoubleBuffering()\n{\n   this.SetStyle(ControlStyles.DoubleBuffer | \n      ControlStyles.UserPaint | \n      ControlStyles.AllPaintingInWmPaint,\n      true);\n   this.UpdateStyles();\n}\n</code></pre>\n'}, {'answer_id': 77041, 'author': 'dummy', 'author_id': 6297, 'author_profile': 'https://Stackoverflow.com/users/6297', 'pm_score': 4, 'selected': False, 'text': '<pre><code>System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control)\n    .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |\n    System.Reflection.BindingFlags.Instance);\naProp.SetValue(ListView1, true, null);\n</code></pre>\n\n<p><a href="https://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form#77233">Ian</a> has some more information about using this on a terminal server.</p>\n'}, {'answer_id': 77055, 'author': 'MagicKat', 'author_id': 8505, 'author_profile': 'https://Stackoverflow.com/users/8505', 'pm_score': 1, 'selected': False, 'text': '<p>You can also inherit the controls into your own classes, and set the property in there.  This method is also nice if you tend to be doing a lot of set up that is the same on all of the controls.</p>\n'}, {'answer_id': 77071, 'author': 'Jeff Hubbard', 'author_id': 8844, 'author_profile': 'https://Stackoverflow.com/users/8844', 'pm_score': 3, 'selected': False, 'text': "<p>One way is to extend the specific control you want to double buffer and set the DoubleBuffered property inside the control's ctor.</p>\n\n<p>For instance:</p>\n\n<pre><code>class Foo : Panel\n{\n    public Foo() { DoubleBuffered = true; }\n}\n</code></pre>\n"}, {'answer_id': 77084, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>Before you try double buffering, see if SuspendLayout()/ResumeLayout() solve your problem.  </p>\n'}, {'answer_id': 77233, 'author': 'Ian Boyd', 'author_id': 12597, 'author_profile': 'https://Stackoverflow.com/users/12597', 'pm_score': 8, 'selected': True, 'text': '<p>Here\'s a more generic version of <a href="https://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form#77041">Dummy\'s solution</a>.  </p>\n\n<p>We can use reflection to get at the protected <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx" rel="noreferrer">DoubleBuffered</a> property, and then it can be set to <strong>true</strong>.</p>\n\n<p><strong>Note</strong>: You should <a href="https://devblogs.microsoft.com/oldnewthing/20050822-11/?p=34483" rel="noreferrer">pay your developer taxes</a> and not <a href="https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793" rel="noreferrer">use double-buffering if the user is running in a terminal services session</a> (e.g. Remote Desktop)  This helper method will not turn on double buffering if the person is running in remote desktop.</p>\n\n<pre><code>public static void SetDoubleBuffered(System.Windows.Forms.Control c)\n{\n   //Taxes: Remote Desktop Connection and painting\n   //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx\n   if (System.Windows.Forms.SystemInformation.TerminalServerSession)\n      return;\n\n   System.Reflection.PropertyInfo aProp = \n         typeof(System.Windows.Forms.Control).GetProperty(\n               "DoubleBuffered", \n               System.Reflection.BindingFlags.NonPublic | \n               System.Reflection.BindingFlags.Instance);\n\n   aProp.SetValue(c, true, null); \n}\n</code></pre>\n'}, {'answer_id': 77331, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 0, 'selected': False, 'text': '<p>I have found that simply setting the DoubleBuffered setting on the form automatically sets all the properties listed here.</p>\n'}, {'answer_id': 89125, 'author': 'Hans Passant', 'author_id': 17034, 'author_profile': 'https://Stackoverflow.com/users/17034', 'pm_score': 6, 'selected': False, 'text': '<p>Check <a href="http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/aaed00ce-4bc9-424e-8c05-c30213171c2c" rel="noreferrer">this thread</a></p>\n\n<p>Repeating the core of that answer, you can turn on the WS_EX_COMPOSITED style flag on the window to get both the form and all of its controls double-buffered.  The style flag is available since XP.  It doesn\'t make painting faster but the entire window is drawn in an off-screen buffer and blitted to the screen in one whack.  Making it look instant to the user\'s eyes without visible painting artifacts.  It is not entirely trouble-free, some visual styles renderers can glitch on it, particularly TabControl when its has too many tabs.  YMMV.</p>\n\n<p>Paste this code into your form class:</p>\n\n<pre><code>protected override CreateParams CreateParams {\n    get {\n        var cp = base.CreateParams;\n        cp.ExStyle |= 0x02000000;    // Turn on WS_EX_COMPOSITED\n        return cp;\n    } \n}\n</code></pre>\n\n<p>The big difference between this technique and Winform\'s double-buffering support is that Winform\'s version only works on one control at at time.  You will still see each individual control paint itself.  Which can look like a flicker effect as well, particularly if the unpainted control rectangle contrasts badly with the window\'s background.</p>\n'}, {'answer_id': 1643053, 'author': 'Chris S', 'author_id': 21574, 'author_profile': 'https://Stackoverflow.com/users/21574', 'pm_score': 3, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form/89125#89125">nobugz</a> gets the credit for the method in his link, I\'m just reposting. Add this override to the Form:</p>\n\n<pre><code>protected override CreateParams CreateParams\n{\n    get\n    {\n        CreateParams cp = base.CreateParams;\n        cp.ExStyle |= 0x02000000;\n        return cp;\n    }\n}\n</code></pre>\n\n<p>This worked best for me, on Windows 7 I was getting large black blocks appearing when I resize a control heavy form. The control now bounce instead! But it\'s better.</p>\n'}, {'answer_id': 14282267, 'author': 'MajesticRa', 'author_id': 548894, 'author_profile': 'https://Stackoverflow.com/users/548894', 'pm_score': 3, 'selected': False, 'text': '<p><strong>Extension method</strong> to turn double buffering on or off for controls</p>\n\n<pre><code>public static class ControlExtentions\n{\n    /// &lt;summary&gt;\n    /// Turn on or off control double buffering (Dirty hack!)\n    /// &lt;/summary&gt;\n    /// &lt;param name="control"&gt;Control to operate&lt;/param&gt;\n    /// &lt;param name="setting"&gt;true to turn on double buffering&lt;/param&gt;\n    public static void MakeDoubleBuffered(this Control control, bool setting)\n    {\n        Type controlType = control.GetType();\n        PropertyInfo pi = controlType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);\n        pi.SetValue(control, setting, null);\n    }\n}\n</code></pre>\n\n<p>Usage (for example how to make DataGridView DoubleBuffered):</p>\n\n<pre><code>DataGridView _grid = new DataGridView();\n//  ...\n_grid.MakeDoubleBuffered(true);\n</code></pre>\n'}, {'answer_id': 17279923, 'author': 'dnennis', 'author_id': 2386550, 'author_profile': 'https://Stackoverflow.com/users/2386550', 'pm_score': 2, 'selected': False, 'text': '<p>This caused me a <strong>lot</strong> of grief for two days with a third party control until I tracked it down.</p>\n\n<pre><code>protected override CreateParams CreateParams\n{\n    get\n    {\n        CreateParams cp = base.CreateParams;\n        cp.ExStyle |= 0x02000000;\n        return cp;\n    }\n}\n</code></pre>\n\n<p>I recently had a lot of holes (droppings) when re-sizing / redrawing a control containing several other controls.</p>\n\n<p>I tried WS_EX_COMPOSITED and WM_SETREDRAW but nothing worked until I used this:</p>\n\n<pre><code>private void myPanel_SizeChanged(object sender, EventArgs e)\n{\n     Application.DoEvents();\n}\n</code></pre>\n\n<p>Just wanted to pass it on.</p>\n'}, {'answer_id': 39343123, 'author': 'Flip70', 'author_id': 6799147, 'author_profile': 'https://Stackoverflow.com/users/6799147', 'pm_score': 2, 'selected': False, 'text': '<p>vb.net version of this fine solution....:</p>\n\n<pre><code>Protected Overrides ReadOnly Property CreateParams() As CreateParams\n    Get\n        Dim cp As CreateParams = MyBase.CreateParams\n        cp.ExStyle = cp.ExStyle Or &amp;H2000000\n        Return cp\n    End Get\nEnd Property\n</code></pre>\n'}, {'answer_id': 67273198, 'author': 'Gregor y', 'author_id': 4496560, 'author_profile': 'https://Stackoverflow.com/users/4496560', 'pm_score': -1, 'selected': False, 'text': '<h2>FWIW</h2>\n<p>building on the work of those who\'ve come before me:<br>\n<a href="https://stackoverflow.com/a/77041/4496560">Dummy\'s Solution</a>, <a href="https://stackoverflow.com/a/77233/4496560">Ian Boyd\'s Solution</a>, <a href="https://stackoverflow.com/a/77023/4496560">Amo\'s Solution</a></p>\n<p>here is a version that sets double buffering via <code>SetStyle</code> in PowerShell using reflection</p>\n<pre><code>function Set-DoubleBuffered{\n&lt;#\n.SYNOPSIS\nTurns on double buffering for a [System.Windows.Forms.Control] object\n.DESCRIPTION\nUses the Non-Public method \'SetStyle\' on the control to set the three\nstyle flags recomend for double buffering: \n   UserPaint\n   AllPaintingInWmPaint\n   DoubleBuffer\n.INPUTS\n[System.Windows.Forms.Control]\n.OUTPUTS\nNone\n.COMPONENT  \nSystem.Windows.Forms.Control\n.FUNCTIONALITY\nSet Flag, DoubleBuffering, Graphics\n.ROLE\nWinForms Developer\n.NOTES\nThrows an exception when trying to double buffer a control on a terminal \nserver session becuase doing so will cause lots of data to be sent across \nthe line\n.EXAMPLE\n#A simple WinForm that uses double buffering to reduce flicker\nAdd-Type -AssemblyName System.Windows.Forms\n[System.Windows.Forms.Application]::EnableVisualStyles()\n\n$Pen = [System.Drawing.Pen]::new([System.Drawing.Color]::FromArgb(0xff000000),3)\n\n$Form = New-Object System.Windows.Forms.Form\nSet-DoubleBuffered $Form\n$Form.Add_Paint({\n   param(\n      [object]$sender,\n      [System.Windows.Forms.PaintEventArgs]$e\n   )\n   [System.Windows.Forms.Form]$f = $sender\n   $g = $e.Graphics\n   $g.SmoothingMode = \'AntiAlias\'\n   $g.DrawLine($Pen,0,0,$f.Width/2,$f.Height/2)\n})\n$Form.ShowDialog()\n\n.LINK\nhttps://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.setstyle?view=net-5.0\n.LINK\nhttps://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.controlstyles?view=net-5.0\n#&gt;\n   param(\n      [parameter(mandatory=$true,ValueFromPipeline=$true)]\n      [ValidateScript({$_ -is [System.Windows.Forms.Control]})]\n      #The WinForms control to set to double buffered\n      $Control,\n      \n      [switch]\n      #Override double buffering on a terminal server session(not recomended)\n      $Force\n   )\n   begin{try{\n      if([System.Windows.Forms.SystemInformation]::TerminalServerSession -and !$Force){\n         throw \'Double buffering not set on terminal server session.\'\n      }\n      \n      $SetStyle = ([System.Windows.Forms.Control]).GetMethod(\'SetStyle\',\n         [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance\n      )\n      $UpdateStyles = ([System.Windows.Forms.Control]).GetMethod(\'UpdateStyles\',\n         [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance\n      )\n   }catch {$PSCmdlet.ThrowTerminatingError($PSItem)}\n   }process{try{\n      $SetStyle.Invoke($Control,@(\n         ([System.Windows.Forms.ControlStyles]::UserPaint -bor\n           [System.Windows.Forms.ControlStyles]::AllPaintingInWmPaint -bor\n           [System.Windows.Forms.ControlStyles]::DoubleBuffer\n         ),\n         $true\n      ))\n      $UpdateStyles.Invoke($Control,@())\n   }catch {$PSCmdlet.ThrowTerminatingError($PSItem)}}\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12597/']