c# - 如何按名称选择 TreeNode?

标签 c# treeview

以下代码不会运行,因为在按名称“RootNode”检索时 rootNode 为 null

 treeView1.Nodes.Add(new TreeNode("RootNode"));

 ...
 //get the rootNode by its name
 TreeView1 rootNode = treeView1.Nodes["RootNode"]

 //rootNode is null so following line throws an error
 rootNode.Nodes.Add(new TreeNode("ChildNode"));

我在这里错过了什么?如何通过名称获取特定节点?

最佳答案

TreeNode 构造函数不接受键/名称参数。索引器基于树节点的名称,而不是其文本。因此,您要么需要设置树节点的名称,要么使用如下不同的添加方法:

treeView1.Nodes.Add("RootNode", "Root Node");

关于c# - 如何按名称选择 TreeNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1124747/

相关文章:

c# - ASP.NET MVC2 生成的 Javascript 包含逗号,该逗号会破坏 Internet Explorer 中的脚本、内部代码

c# - 在不同的方法中使用数组

c++ - Win32 C++ : How to get number of checked items in treeview control

c# - 为什么我的 TreeView 不更新?

c# - 如何防止RichTextBox Append 导致OutOfMemory?

c# - 使用 Thread.Abort() 有什么问题

c# - 尝试在 TextBlock 中内联 HyperLinkBut​​ton

.net - WPF TreeView : How to display rounded border around the selected item?

c# - TreeView 节点的图像索引在选择时发生变化

php - 当我只有父级的父级文本时如何获取 TreeView 项目的 id