How can I DoubleBuffer a winforms Treeview?

I have a standard winforms treeview control that keeps flickering whenever I hover my mouse over any other control on the form. I would like to doubleBuffer the treeview to reduce the flickering but I have no idea how to do so. Could someone please show me how to achieve my goal?

Many thanks

I had to implement a double buffered TreeView a way back when developing some parts of a financial software, because of the same scenario. The TreeView implementation in .NET is a pretty sketchy one, but here is how I resolved it:

 Public Class DoubleBufferedTreeView
    Inherits System.Windows.Forms.TreeView

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.UpdateStyles()
    End Sub
End Class

The other reason I implemented this in this manner was because I had to do some custom drawing to show where the user was dragging-and-dropping the TreeNodes, so I did some custom drawing to display a bar in between nodes.

DoubleBuffering was not a fullproof solution as the TreeView flickered slightly, but that was the best I was able to get it at the time. I also did not want to suspend the TreeView as others have stated, because I still wanted the TreeView to perform its layout and normal operations, even when the user was possibly using different parts of the UI.

PS. the code is almost identical for C#.

TreeView.DoubleBuffered Property (System.Windows.Forms , The DoubleBuffered property does not affect the TreeView control. protected: virtual property bool DoubleBuffered { bool get(); void set(bool value); };. C# Copy​. protected override bool DoubleBuffered { get; set; } member this.DoubleBuffered : bool with get, set Protected Overrides Property DoubleBuffered As Boolean Property Value Boolean. true if the control uses a secondary buffer; otherwise, false. Remarks. Setting the DoubleBuffered property does not affect the TreeView control.

All answers here are wrong. The Treeview ignores

SetStyle(ControlStyles.AllPaintingInWmPaint,  true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

completely.

And using BeginUpdate() or SuspendLayout() does not change anything.

The correct answer was given by Hans Passant here: Treeview flickering?

Double-buffered Tree and Listviews, Implementing native WinForms flicker-free TreeView and ListView public DbTreeView() { // Enable default double buffering processing  If you ever use default WinForms TreeView or ListView controls, you already notice massive flickering when control size changes (for example, when you change form size with mouse). In this article, I will try to find workarounds for this behavior and eliminate flickering by using double buffering.

In Addition to activating the double buffering like Int3 already said, I suggest you to suspend the layout logic temporarily while you do the processing that causes the flickering.

You need to first call SuspendLayout to stop building the full tree contents in th UI. After you have finished your processing, you start the layout logic again eith ResumeLayout(). MSDN documentatiion for SuspendLayout with code sample is here.

private void buildTreeContent()
{
   // Suspend the form layout and add two buttons.
   this.SuspendLayout();

   // Do your work here
   // ...

   // Make the Form do paint the layout again.
   this.ResumeLayout();
}

This should help a great deal of flicker as building the tree element is resource consuming, we've done that a lot of times in our projects.

An alternative approach is to work with the Windows messages. This is explained in more depth in another SO thread.

Double-buffered Tree and List views, If you ever use default WinForms TreeView or ListView controls you already notice massive for this behavior and eliminate flickering by using double buffering. Standard Windows Forms controls are double buffered by default. You can enable default double buffering in your forms and authored controls in two ways. You can either set the DoubleBuffered property to true, or you can call the SetStyle method to set the OptimizedDoubleBuffer flag to true.

DoubleBuffer does not affect Treeview in .NET. If your purpose is to reduce flicker when the TreeView is drawn, then I suggest you have a look at BeginUpdate and EndUpdate.

I had a similar issue, and attempted to double buffer the form in hopes of it fixing the issue with my treeview. As it turns out, setting the DoubleBuffered property does not affect the TreeView control.

Hope this helps. Useful link

C# 2.0 WinForms: Prevent flickering when updating TreeView nodes , After doing some searching on double buffering the TreeView it appears that that is not the answer. Deriving your own TreeView control and  Using the recommendations outlined here seems to work, which sets the TVS_EX_DOUBLEBUFFER extended window style available since Vista: Treeview flickering? However, I would much prefer not to have the extra dependency on user32.dll as the project is set to also run on Mono.

Hope you are ok with the C# code as it is trivial. DoubleBuffer is actually a protected property of the Control. So you can only access it from the declaring class or that are derived. Following snippet will help you understand how to set this property.

public sealed class MyNonFlickringTreeView:Treeview
{
 public MyNonFlickringTreeView()
   {
      this.DoubleBuffered=true;
   }
}

Set DoubleBuffered Property [C#], DoubleBuffered to true. This is useful, if you want to avoid flickering of controls such as ListView (when updating) or Panel (when you draw on it). 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.

Pro .NET 2.0 Windows Forms and Custom Controls in C#, More Advanced TreeViews The TreeView is a sophisticated control, and it DoubleBuffered, and the DrawNode event Sorted and TreeViewNodeSorter  It can trivially produce nice reports from the ListView. It supports all ListView views (report, tile, large and small icons). It supports owner drawing, including rendering animated GIFs. Its columns can be fixed-width or limited to a minimum/maximum. It shows a highly customisable "list is empty" message when the list is empty (obviously).

vb.net, I have a treeview derived control placed within Panel2 of a would not stop flickering), even with C# setstyle (UserPaint, AllPaintinginWMPaint,  Now, you see, if i select an event (in left treeview) and then try to select something in the right treeview, the left treeview will stop showing the blue selection rectangle. This is obviously bad because now the user doesnt know which event is he editing!

Double Buffer panel within SplitContainer to avoid TreeView flicke , Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to add treeview selected items inside the datagridview columns as a treeview c# [duplicate]

Comments
  • Do you have any mouse over/hover events that might be causing it to redraw itself, or is the flicker standard behaviour of the control?
  • i have no mouse over/hover events so flickering is standard behaviour.
  • possible duplicate of How to double buffer .NET controls on a form?
  • @Bala R: I've already tried the code in the link you provided and had no success
  • Have a look here: dev.nomad-net.info/articles/double-buffered-tree-and-list-views
  • unfortunetly, your code had no effect at all. The treeview still flickers.
  • The treeview is being populated from contents of HDD. During population there is no flicker and the whole process takes less than a second. So suspend/resume layout does not help me. The treeview flicker occurs when the mouse hovers over other controls and no events are fired that involve the treeview.
  • This answer is wrong. SuspendLayout() does not change anything. My treeview flickers entirely when I only select a node, not when the tree is populated with nodes.
  • Why do you post this non-solution?
  • The treeview has no DoubleBuffer property.
  • It looks like DoubleBuffered is not working/implemented for the TreeView