BUG: Node element does not paint after you call the EndUpdate method (814354)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

In a Microsoft Visual Studio .NET Windows application, you call the Nodes.Clear method to clear the nodes of a TreeView control before you call the TreeView.endUpdate method. The first node added after TreeView.EndUpdate does not paint until you add the second node.

CAUSE

When you call the EndUpdate method, a WM_SETREDRAW message is delivered to redraw the nodes of the TreeView control. When you call Nodes.Clear before EndUpdate, the WM_SETREDRAW message sets the number of nodes in TreeView to -1 instead of 0. When you add the first node element, the node count is incremented to 0 instead of 1. Therefore, the TreeView node is not painted. When you add a second node, the node count is incremented to 1, the paint method is called, and the node count is correctly updated to 2. This causes both the nodes to be painted.

WORKAROUND

To work around this problem, add a node to the TreeView, and then send a WM_SETREDRAW message to reset the node count to 1 instead of 0. To do this, call the Nodes.Add method between the BeginUpdate method and the EndUpdate method, as follows:

Visual Basic .NET
TreeView1.BeginUpdate()
TreeView1.Nodes.Add("test")
TreeView1.EndUpdate()
Visual C# .NET
treeView1.BeginUpdate(); 
treeView1.Nodes.Add("test") ;
treeView1.EndUpdate(); 

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Open Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project types, click to select Visual Basic Projects or Visual C# Projects.
  4. Under Templates, click to select Windows Application.

    By default, Form1 is created.
  5. On the View menu, click Toolbox.
  6. Click Windows Forms.
  7. Drag the TreeView control to Form1.

    TreeView1 is created on Form1.
  8. From the toolbox, drag a Button control to Form1.

    Button1 is created on Form1.
  9. From the toolbox, drag another Button control to Form1.

    Button2 is created on Form1.
  10. In the Design view, double-click Button1, and then add the following code in the click event:

    Visual Basic .NET
     TreeView1.Nodes.Add("test")
    
    Visual C# .NET
    treeView1.Nodes.Add("test") ;
  11. In Solution Explorer, right-click Form1, and then click View Designer.
  12. In the Design view, double-click Button2, and then add the following code in the click event:

    Visual Basic .NET
    TreeView1.BeginUpdate()
    TreeView1.Nodes.Clear()
    TreeView1.EndUpdate()
    
    Visual C# .NET
    treeView1.BeginUpdate();
    treeView1.Nodes.Clear() ;
    treeView1.EndUpdate(); 
  13. On the Debug menu, click Start to run the project.
  14. Click Button1 to add a node to the TreeView.
  15. Click Button2 to clear the node that you added.
  16. Click Button1 to add the node again.

    Notice that the new node is not visible.
  17. Click Button1.

    Two nodes are displayed in TreeView.

REFERENCES

For more information, see your .NET Framework SDK Documentation or visit the following MSDN Web site:

TreeView Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsTreeViewClassTopic.asp

Modification Type:MinorLast Reviewed:1/20/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbvs2002sp1sweep kbTreeView kbControl kbComCtrls kbbug KB814354 kbAudDeveloper