【问题标题】:Insert Existing TreeNodeCollection in TreeNode在 TreeNode 中插入现有的 TreeNodeCollection
【发布时间】:2013-05-10 17:55:59
【问题描述】:
问题:
在下面的屏幕截图中,我有一个节点 300-9885-00X 及其 TreeNodeCollection(在红色方块中)。再低一点,我们又找到了300-9885-00X,我想把之前找到的TreeNodeCollection插入到那个节点中……
背景资料
我有一个遍历 AutoCAD / SolidEdge 程序集的递归程序。它打开文档并打印程序集及其子项,等等(递归)...
- 绿色表示已打印
- 橙色表示之前已经打印过了,不需要再打印了...
问题:
我们如何将现有的 TreeNodeCollection 插入到 TreeNode 中?
知道:
- TreeNodeCollection 的位置
- 我要将集合插入到的节点的位置
以下变量TreeNodes 包含我的收藏。我必须遍历集合才能添加其文本吗?
【问题讨论】:
标签:
vb.net
insert
treeview
treenode
treenodecollection
【解决方案1】:
您不能将TreeNodeCollection 添加到节点。您必须循环通过TreeNodeCollection 并单独添加节点,如下所示:
For j As Integer = 0 To TreeNodes.Count - 1
n.Nodes.Add(TreeNodes(j).Clone())
Next
注意我使用了.Clone()。这是由于插入了一个已经存在的节点。您不能这样做,您必须删除现有的或克隆它。就我而言,我必须克隆它。
【解决方案2】:
// Get the '_NodesCollection' from the '_parentNode' TreeNode.
TreeNodeCollection _Nodes = _parentNode.FirstNode.Nodes;
// Create an array of 'TreeNodes'.
TreeNode[] Nodes = new TreeNode[_Nodes.Count];
// Copy the tree nodes to the 'Nodes' array.
_Nodes.CopyTo(Nodes, 0);
// Remove the First Node & Children from the ParentNode.
_parentNode.Nodes.Remove(_parentNode.FirstNode);
// Remove all the tree nodes from the '_parentNode' TreeView.
_parentNode.Nodes.Clear();
// Add the 'Nodes' Array to the '_parentNode' ParentNode.
_parentNode.Nodes.AddRange(Nodes);
// Add the Child Nodes to the TreeView Control
TvMap.Nodes.Add(_parentNode);