Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treeview flickering?

Tags:

I came to know that by adding TreeView.BeginUpdate will prevent flickering of treeview, but when i added it in to my project all nodes of my treeview disappears, Can any body tell me why it happens, here is the code snippet where i used TreeView.BeginUpdate and TreeView.EndUpdate

  TreeNode treeNode = new TreeNode("Windows");         treeView1.Nodes.Add(treeNode);         //         // Another node following the first node.         //         treeNode = new TreeNode("Linux");         treeView1.Nodes.Add(treeNode);         //         // Create two child nodes and put them in an array.         // ... Add the third node, and specify these as its children.         //         TreeNode node2 = new TreeNode("C#");         TreeNode node3 = new TreeNode("VB.NET");         TreeNode[] array = new TreeNode[] { node2, node3 };         //         // Final node.         //         treeNode = new TreeNode("Dot Net Perls", array);         treeView1.Nodes.Add(treeNode); 
like image 508
vettori Avatar asked Apr 28 '12 11:04

vettori


People also ask

What is TreeView in C#?

The TreeView control contains a hierarchy of TreeViewItem controls. It provides a way to display information in a hierarchical structure by using collapsible nodes . The top level in a tree view are root nodes that can be expanded or collapsed if the nodes have child nodes.

How do you edit TreeView?

The TreeView allows you to edit nodes by setting the allowEditing property to true. To directly edit the nodes in place, double click the TreeView node or select the node and press F2 key. When editing is completed by focus out or by pressing the Enter key, the modified node's text saves automatically.

What is TreeNode C#?

The TreeNode label is set by setting the Text property explicitly. The alternative is to create the tree node using one of the TreeNode constructors that has a string parameter that represents the Text property. The label is displayed next to the TreeNode image, if one is displayed.


1 Answers

The Begin/EndUpdate() methods were not designed to eliminate flicker. Getting flicker at EndUpdate() is inevitable, it repaints the control. They were designed to speed-up adding a bulk of nodes, that will be slow by default since every single item causes a repaint. You made it a lot worse by putting them inside the for loop, move them outside for an immediate improvement.

That will probably be sufficient to solve your problem. But you can make it better, suppressing flicker requires double-buffering. The .NET TreeView class overrides the DoubleBuffered property and hides it. Which is a historical accident, the native Windows control only supports double buffering in Windows XP and later. .NET once supported Windows 2000 and Windows 98.

That's not exactly relevant anymore these days. You can put it back by deriving your own class from TreeView. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing TreeView. The effect is very noticeable, particularly when scrolling.

using System; using System.Windows.Forms; using System.Runtime.InteropServices;  class BufferedTreeView : TreeView {     protected override void OnHandleCreated(EventArgs e) {        SendMessage(this.Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);         base.OnHandleCreated(e);     }     // Pinvoke:     private const int TVM_SETEXTENDEDSTYLE = 0x1100 + 44;     private const int TVM_GETEXTENDEDSTYLE = 0x1100 + 45;     private const int TVS_EX_DOUBLEBUFFER = 0x0004;     [DllImport("user32.dll")]     private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); } 
like image 116
Hans Passant Avatar answered Sep 29 '22 11:09

Hans Passant