当我添加到 C# 树时,它正在复制每个节点

标签 c# treeview tree duplicates

所以基本上我所做的是向后构建一棵树。我从叶子开始,然后添加他们的 parent ,然后是他们的 parent (最后是一个 3 级树)。

我添加叶子没问题(我查询数据库,并为每个条目创建一个 TNode(TreeNode 类的扩展),然后将它们添加到树中)。这一步没有重复——当我去添加叶子的 parent 时,重复发生了。

我遍历叶子树,当两个叶子具有不同的节号(只是我想分组的数字)时,几乎可以创建一个新的父节点。

程序正在做的是在每个父节点下为每个叶节点创建两组,所以它看起来像这样:

它不会让我张贴图片,所以我会尝试用文字画树:像这样

    Part 1
       child 1-1
       child 1-2
       child 1-3
       child 1-1
       child 1-2
       child 1-3
    Part 2
       child 2-1
       child 2-2       
       child 2-1
       child 2-2  

Here is the code that is doing it...It is really wierd, cause when I debug it, the number of children of each node grows by ONE each time as expected, but then once all the parent nodes are added, there is two of each leaf...

I have also tried cutting count down to be just 1, and it adds ONE parent, and it has ONE child, which is duplicated....

Anyways, Code:

Note the switch statements arent really important, they just set the text of the new node

private void addLvl1Nodes() {

    int i = 0;
    TNode newNode = null;
    int count = tv_master.Nodes.Count;
    while (i < count) {
        TNode tn = (TNode)tv_master.Nodes[i];//get current node
            //if i = 0, then newNode has not been set, so make a new one!
            if ((i == 0)||
                (tn.Section != ((TNode)tv_master.Nodes[i-1]).Section )||
                (tn.Text.Substring(0, 1) != tv_master.Nodes[i - 1].Text.Substring(0, 1))){

                newNode = new TNode("");
                tv_master.Nodes.Add(newNode);
                switch (tn.Text.Substring(0, 1)) {
                    case "1": newNode.Text = "Part One"; break;
                    case "2": newNode.Text = "Part Two"; break;
                    case "3": newNode.Text = "Part Three"; break;
                }

                newNode.Section = tn.Section;

            }
        newNode.Nodes.Add(tn);

        i++;
    }
}

希望之前有人遇到过类似的错误 - 因为我就是找不到解决方案。

非常感谢!

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

对于遇到类似问题的任何人,这是我的代码的最终副本,现在按预期工作:)

private void addLvl1Nodes() {

            TNode newNode = null;

            List<TNode> nodes = tv_master.Nodes.Cast<TNode>().ToList();
            tv_master.Nodes.Clear();

            for(int i = 0; i<nodes.Count; i++){
                TNode tn = nodes[i];

                if ((i == 0) ||
                    (tn.Section != nodes[i-1].Section) ||
                    (tn.Text[0] != nodes[i-1].Text[0])) {

                    newNode = new TNode("");
                    tv_master.Nodes.Add(newNode);
                    switch (tn.Text[0]) {
                        case '1': newNode.Text = "Part One"; break;
                        case '2': newNode.Text = "Part Two"; break;
                        case '3': newNode.Text = "Part Three"; break;
                    }

                    newNode.Section = tn.Section;

                }

                newNode.Nodes.Add(tn);

            }
        }

最佳答案

看起来您可能需要在进行此调用之前从它的旧父项中删除 tn:

  newNode.Nodes.Add(tn);

否则,您实际上不会有两个单独的 tn 实例(子节点),但它会在层次结构中出现不止一次。 (一次直接在tv_master下,一次在新创建的节点下。)

只保留两个单独的节点列表(或树)可能有些意义。一个保存新创建的父节点,一个保存叶节点。然后你可以在最后加入所有东西。也许像这样开始你的循环:

List<TNode> listLeafNodes = new List<TNode>(tv_master.Nodes);
tv_master.Nodes.Clear();
for( int i=0; i<listLeafNodes.Count; ++i )
{
...
}

当然,我不确定您为 TNode 和 tv_master 使用的是什么类。我在 MSDN 上看到的 TNode 接口(interface)似乎没有任何 .Nodes 属性。此外,似乎至少有 3 个不同的 TreeView 类。 TNode 的完全限定类名和 tv_master 的定义可能有助于我们更好地理解事情。

关于当我添加到 C# 树时,它正在复制每个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5914228/

相关文章:

r - 使用插入符号库修剪树返回复杂的树

c# - EF 迁移 - 数据库中已经有一个名为 '' 的对象

c# - C# 委托(delegate)可以使用对象类型来更通用吗?

c# - 如何在 C# 中查找驱动器的可用百分比

treeview - getNodeText() 不返回对象管理器搜索树的文本

c# - e.slice 不是 ASP.NET 中的函数

c# - 在 winforms TreeView 中设置第一个节点的位置/边距

c# - MVVM WPF 中的 ICommand

algorithm - 在二维图中放置互连节点的最佳方法是什么?

c++ - 在 C++ 中创建非二叉树结构