4

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

mazrabul
  • 295
  • 1
  • 5
  • 17
  • 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? – John K May 25 '11 at 02:20
  • i have no mouse over/hover events so flickering is standard behaviour. – mazrabul May 25 '11 at 02:21
  • 1
    possible duplicate of [How to double buffer .NET controls on a form?](http://stackoverflow.com/questions/76993/how-to-double-buffer-net-controls-on-a-form) – Bala R May 25 '11 at 03:50
  • @Bala R: I've already tried the code in the link you provided and had no success – mazrabul May 25 '11 at 04:08
  • @mazrabul Did you try the code in Hans's answer as well? – MarkJ May 25 '11 at 08:52
  • @MarkJ: yes I did. The soultion provided was the first thing I tried. It did not solve my flicker problem but it did solve another issue I was having with form loading. The form that the treeview is part of now loads faster and better because of his solution but no flicker fix. – mazrabul May 25 '11 at 19:06
  • Have a look here: http://dev.nomad-net.info/articles/double-buffered-tree-and-list-views – Edwin Groenendaal May 25 '11 at 06:24

5 Answers5

1

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?

Elmue
  • 7,602
  • 3
  • 47
  • 57
1

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#.

David Anderson
  • 13,558
  • 5
  • 50
  • 76
0

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

foyss
  • 973
  • 2
  • 8
  • 24
0

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.

Community
  • 1
  • 1
Jens H
  • 4,590
  • 2
  • 25
  • 35
  • 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. – mazrabul May 25 '11 at 19:00
  • 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. – Elmue Oct 23 '19 at 15:04
-1

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;
   }
}
crypted
  • 10,118
  • 3
  • 39
  • 52