How to fix: “last item not visible in TreeView”

As reported this problem is somehow related to Visual Styles usage in .Net. This is known issue and is recognized by Microsoft. There are a few ways to fix this problem. Let’s start with the easiest one. Note that this problem occurs in cases when treeView is dynamically populated.

Disabling visual styles is the easy solution, but not suitable in case you need visual styles to be enabled.

Another solution is to always use treeView.BeginUpdate() (before) and treeView.EndUpdate()  (after) you populate your treeView. This not only that solves the problem with the missing last item it also improves performance. But it should be used carefully and watch out not to overuse it . Here is a bad example:

foreach (TreeNode node in treeView.Nodes)
{
    treeView.BeginUpdate();
    node.Text += node.Text + (" (Selected) ");
    treeView.EndUpdate();
}

Calling treeView.EndUpdate() causes the treeView to be redrawn but if we call it to often we end up with the same issue we had in the first place, performance.
Correct example would be:

treeView.BeginUpdate();
foreach (TreeNode node in treeView.Nodes)
{
    node.Text += node.Text + (" (Selected) ");
}
treeView.EndUpdate();

Hope this helps someone struggling with the similar issue.
If anyone has something to add or improve post a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




Recent Posts

GiottoPress by Enrique Chavez