Search code examples
c#winformstreeviewcontrolsscrollbar

TreeView vertical scrollbar does not expand to the last node, last node is hidden?


I am experiencing an odd behaviour from a TreeView control.

enter image description here

As you can see, the last node in the treeview is somehow hidden (although I can select it with keyboard up/down arrow keys) and the scroll bar does not expand to it. In the picture the blue line you see is the selected node that is hidden but luckily still a bit of highlight is visible!

Even when I press pagedown or END keys, I cannot select the last node!

I tried to change height of the treeview control to match the height of nodes but still no success! My treeview is inside a 'panel'. could it be the problem?

UPDATE I am not doing something extraordinary. I just populate with a loop over a List<> (using suspend and resume before and after loop) then the treeview will be populated normally.

public class MyNode
{
public string Name {get;set;}
public string Result {get;set;} 
}
//suspending code here (dont have access to paste it here)
    foreach(MyNode node in myNodeList)
    {
    TreeNode tn = new TreeNode();
    tn.Text = node.Name;
    tn.Name = node.Result;
    treeView.Nodes.Add(tn);
    }
////unsuspending code here (dont have access to paste it here)

Later I allow user to press a button to highlight the nodes that have result set to 'fail':

foreach(TreeNode node in treeView.Nodes)
{
if (node.Name.ToString() == "fail") node.BackColor = Color.Red;
}

After this, the last node in the treeview is going hidden!!!

FIX I used BeginUpdate() and EndUpdate() methods and the problem is gone!!!


Solution

  • I found out that I have to user treeView.BeginUpdate() and treeView.EndUpdate() methods before and after populating the tree view and also making any changes to its nodes.