c# - 在 Windows 应用程序中以编程方式选择 TreeView 中的节点

标签 c# .net windows winforms treeview

我已经加载了一个 TreeView 。我想遍历 TreeView 节点并展开并选择一个节点。展开工作正常。但选择一个节点不起作用。

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 应用程序中。

最佳答案

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

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.

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

node.TreeView.SelectedNode = node.NextNode;  

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

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.

关于c# - 在 Windows 应用程序中以编程方式选择 TreeView 中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7736457/

相关文章:

c# - 检查文件夹中的文件

python - 如何更改 pinax(0.9a2) 模板?

c++ - 如何使用 C++ 在 SYSTEM 进程中获取事件用户名?

c# - 在 .NET 中使用来自另一个程序集的抽象类

c# - 如何动态创建一个类

c# - 批量插入节点和关系 neo4jclient

c# - 根据另一个下拉列表中的选择填充下拉列表

c# - 从Youtube链接检索视频流

c# - 构建存储库时的 Entity Framework Core 工具

windows - 在 Windows 终端中的 git 帐户之间切换的简单方法?