Scroll selected TreeView node into view

2019-04-18 01:39发布

I have a System.Windows.Forms.TreeView docked inside a panel. I am setting a node selected programmatically. What method or property would I use to have the treeview scroll the selected into view?

3条回答
SAY GOODBYE
2楼-- · 2019-04-18 02:01

I had some issues with node.EnsureVisible() not working for trees with only one level of nodes.

To fix this use the BindingIndex to identify the node selected. Then the node selected will be scrolled in view.

The example shows myTable from a LINQ query.

node.BindingIndex = Convert.ToInt32(mytable.Id);

I hope this helps some of you.

查看更多
\"骚年 ilove
3楼-- · 2019-04-18 02:06

I also had issues with this and figured out that treeview.ExpandAll() ignores the EnsureVisible() effect and avoids the scrolling to the node position.

Just call EnsureVisible() after ExpandAll() if you want a full expanded tree with the scroll on the node you've selected.

查看更多
Animai°情兽
4楼-- · 2019-04-18 02:21
node.EnsureVisible();

for example:

if(treeView.SelectedNode != null) treeView.SelectedNode.EnsureVisible();

(see MSDN)

查看更多
登录 后发表回答