如何基于字符串路径以编程方式选择或滚动到 TreeView 中的 TreeNode?
我正在实现 TreeView 控件的常见场景之一,并且使用驱动器、文件和文件夹。制作文件系统。这意味着每个节点可能都有一条路径。
问题是,我们有 EnsureVisible 方法,但并不完全相信它的作用与它所说的一样。没有明确的“setVisible”为 false 属性。这意味着默认情况下所有 TreeNode 都是可见的!
有人能想出解决方案来证明它确实有效吗?
这是我正在使用的方法存根吗?
public void selectTreeNodeFromPath(string path)
{
//char[] delimiters = new char[]{ '\\' };
//string[] pathArray = path.Split(delimiters);
//int numberOfLvlsToProbe = pathArray.Length;
// foreach (TreeNode node in treeDrives.Nodes)
// {}
}
你可以看到,我已经开始解决这个问题,但在一个简单的测试用例没有产生任何效果后,我遇到了障碍!
I am implementing one of the common scenarios of a TreeView control and I am using a drives, files and folders. to make a File System. That means each node potentially has a path.
Problem is, we have the ensureVisible method but am not entirely convinced this does what it says it does. There is no explicit 'setVisible' to false property. This meas by default all TreeNodes are going to be Visible !!!
Can someone come up with solution that proves it does work?
Heres a method stub am working with?
public void selectTreeNodeFromPath(string path)
{
//char[] delimiters = new char[]{ '\\' };
//string[] pathArray = path.Split(delimiters);
//int numberOfLvlsToProbe = pathArray.Length;
// foreach (TreeNode node in treeDrives.Nodes)
// {}
}
You can see that I have I started to attack this problem but I have hit a tumbling block after a simple test case yielded NO-EFFECT !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TreeNode 的可见性取决于其父节点的展开情况以及控件的滚动位置。 EnsureVisible 方法的作用是展开适当的父节点并将控件滚动到指定的节点。
假设您的树已填充了节点,您应该能够在最后一个节点上调用
EnsureVisible
,并且控件将展开所有父级。然后您可以设置SelectedNode
以选择该节点。TreeNodes will be visible depending on the expanded condition of their parent nodes and where the scroll position of the control is. What the EnsureVisible method does is expand the proper parents and scroll the control to the specified node.
Assuming that your tree has already been populated with the nodes, you should be able to call
EnsureVisible
on the last node and the control will expand all the parents. Then you can setSelectedNode
to have that node be selected.解决方案如下:
正在工作并经过测试:
Heres the solution:
WORKING and TESTED: