I have huge tree, about 1000 nodes, and when I use TreeView.ExpandAll method it scrolling all tree to the end not smoothly. How to make it scrolling smoothly or not scrolling at all (let remain in the beggining) ?
By using the BeginUpdate() and EndUpdate() methods of the TreeView control, you can suspend the drawing of the TreeView control while you add the nodes to the tree. The ExpandAll() method of the TreeView control seems to ignore that part when it comes to displaying the scrollbar portion of the control.
A simple work-around is to just expand the parent node before adding the node branch to the TreeView control while using those BeginUpdate() and EndUpdate() methods.
Example:
treeView1.BeginUpdate();
for (int i = 0; i < 4; ++i) {
TreeNode tn = new TreeNode("Node #" + i.ToString());
tn.Expand();
for (int j = 0; j < 250; ++j) {
tn.Nodes.Add("Child #" + j.ToString());
}
treeView1.Nodes.Add(tn);
}
treeView1.EndUpdate();
I actually recently had to solve this problem. All I did after calling the ExpandAll() method was set the SelectedNode in the TreeView to the first node, like this :
this.treeView1.ExpandAll();
this.treeView1.SelectedNode = this.treeView1.Nodes[0];
You want to be careful because this will probably throw an exception if there are 0 nodes in your TreeView so it would be a good idea to place it in a try-catch block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With