Microsoft KB Archive/814354

= BUG: Node element does not paint after you call the EndUpdate method =

Article ID: 814354

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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(&quot;test&quot;) TreeView1.EndUpdate Visual C# .NET treeView1.BeginUpdate; treeView1.Nodes.Add(&quot;test&quot;) ; treeView1.EndUpdate;



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



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

By default, Form1 is created. On the View menu, click Toolbox. Click Windows Forms.</li> Drag the TreeView control to Form1.

TreeView1 is created on Form1.</li> From the toolbox, drag a Button control to Form1.

Button1 is created on Form1.</li> From the toolbox, drag another Button control to Form1.

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

Visual Basic .NET TreeView1.Nodes.Add(&quot;test&quot;) Visual C# .NET treeView1.Nodes.Add(&quot;test&quot;) ; </li> In Solution Explorer, right-click Form1, and then click View Designer.</li>  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; </li> On the Debug menu, click Start to run the project.</li> Click Button1 to add a node to the TreeView.</li> Click Button2 to clear the node that you added.</li> Click Button1 to add the node again.

Notice that the new node is not visible.</li> Click Button1.

Two nodes are displayed in TreeView.</li></ol>

<div class="references_section">