在 Windows 应用程序中以编程方式选择树视图中的节点
我已经加载了树视图。我想遍历树视图节点并展开&选择一个节点。扩展工作正常。但选择一个节点不起作用。
private void Traverse(TreeNodeCollection nodes, string findtext)
{
foreach (TreeNode node in nodes)
{
if (node.Text.ToString().Trim() == findtext)
{
node.Expand();
node.TreeView.SelectedNode = node.NextNode;
//tvwStructureTree.SelectedNode = this.tvwStructureTree.Nodes[node.Index];
//Select a node in Treeview tvwStructureTree But not working
tvwStructureTree.SelectedNode = node;
node.TreeView.Focus();
}
Traverse(node.Nodes, findtext);
}
}
它在Windows应用程序中。
i have load a tree view. i want to Traverse treeview node and expand & select a node. Expand is working fine. but select a node is not working.
private void Traverse(TreeNodeCollection nodes, string findtext)
{
foreach (TreeNode node in nodes)
{
if (node.Text.ToString().Trim() == findtext)
{
node.Expand();
node.TreeView.SelectedNode = node.NextNode;
//tvwStructureTree.SelectedNode = this.tvwStructureTree.Nodes[node.Index];
//Select a node in Treeview tvwStructureTree But not working
tvwStructureTree.SelectedNode = node;
node.TreeView.Focus();
}
Traverse(node.Nodes, findtext);
}
}
Its in windows application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我完全测试了您自己的代码并且工作正常,找到并选择了节点!
我的树视图没有任何特定的属性设置!
顺便说一句,我正在使用 .Net 3.5 和 VS 2008
I tested exactly your own code and worked fine, both find and selection the node!
without any particular property setting for my treeview!
by the way I am using .Net 3.5 and VS 2008
不太确定你的问题是什么。 TreeView.SelectedNode 属性 是正确的财产。
确保
node.TreeView
是正确的 TreeView 实例。TreeView.HideSelection 属性 是另一个属性这可能对你有用。
Not quite sure what's your issue is. TreeView.SelectedNode Property is the correct property.
Make sure that the
node.TreeView
is the correct TreeView instance.TreeView.HideSelection Property is another property that might useful for you.
我有类似的问题。我的表单的构造函数接受了要选择的节点的测试。找到正确的节点不是问题,但树没有将节点显示为选定的,因为树控件没有焦点。在设置
myTreecontrol.SelectedNode
之前只需使用Form.ActiveControl = myTreecontrol;
I had a similar issue. My form's ctor is given the test of a node to select.Finding the correct node was not a problem, but the tree didn't show the node as selected, since the tree control didn't have focus. merely had to use
Form.ActiveControl = myTreecontrol;
before settingmyTreecontrol.SelectedNode