在 Windows 应用程序中以编程方式在树视图中选择一个节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7736457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:15:21  来源:igfitidea点击:

select a node in treeview programmatically in windows application

c#.netwindowswinformstreeview

提问by user990897

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.

它在 Windows 应用程序中。

回答by CharithJ

Not quite sure what's your issue is. TreeView.SelectedNode Propertyis the correct property.

不太确定你的问题是什么。TreeView.SelectedNode 属性是正确的属性。

When you set this property, the specified node is scrolled into view and any parent nodes are expanded so that the specified node is visible.

When the parent node or any ancestor node of the selected node is collapsed either programmatically or through user action, the collapsed node becomes the selected node.

设置此属性时,指定的节点会滚动到视图中,并且所有父节点都会展开,以便指定的节点可见。

当所选节点的父节点或任何祖先节点以编程方式或通过用户操作折叠时,折叠的节点将成为所选节点。

Make sure that the node.TreeViewis the correct TreeView instance.

确保node.TreeView是正确的 TreeView 实例。

node.TreeView.SelectedNode = node.NextNode;  

TreeView.HideSelection Propertyis another property that might useful for you.

TreeView.HideSelection 属性是另一个可能对您有用的属性。

When this property is set to false, selected nodes in the TreeView control remain highlighted in a different color than the current selection color when the TreeView control loses focus. You can use this property to keep items that are selected by the user visible when the user clicks a different control on the form or moves to a different window.

当此属性设置为 false 时,当 TreeView 控件失去焦点时,TreeView 控件中的选定节点将保持以与当前选择颜色不同的颜色突出显示。当用户单击窗体上的不同控件或移动到不同的窗口时,您可以使用此属性使用户选择的项目保持可见。

回答by Geno Carman

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 setting myTreecontrol.SelectedNode

我有一个类似的问题。我的表单的 ctor 接受了要选择的节点的测试。找到正确的节点不是问题,但是树没有将节点显示为选定的,因为树控件没有焦点。只需要Form.ActiveControl = myTreecontrol;在设置前使用myTreecontrol.SelectedNode

回答by PureSilence

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

我完全测试了您自己的代码并且工作正常,找到并选择节点!我的树视图没有任何特定的属性设置!顺便说一下,我正在使用 .Net 3.5 和 VS 2008