【问题标题】:Can't detect duplicate treeview nodes and assign it as child properly无法检测到重复的树视图节点并将其正确分配为子节点
【发布时间】:2014-04-25 14:11:41
【问题描述】:

我有一个方法用作线程安全回调来更新树视图。它需要两个字符串参数。第一个是传递的数据,第二个是它检查的主机的 IP。

我正在尝试检查树视图当前是否包含包含输入字符串的字符串,如果不包含,则应该将其作为父节点添加到树视图中,然后在下面添加 ip 字符串作为孩子。尽管如果它已经包含该输入字符串作为父节点,那么它应该只在数据字符串匹配的父节点下方添加 IP 地址。所以它基本上对ips进行排序。每个父节点下面会有多个ip。

我的问题是我的方法是将每个字符串添加为它自己的父项,无论它是否是重复的,这也意味着它没有在父项下添加重复输入的 IP。谁能看看我哪里出错了?

 public void UpdateScan(string input, string ip)
        {
            lock (outputTree)
            {
                outputTree.BeginUpdate();

                if (!(outputTree.Nodes.ContainsKey(input)))
                {
                    TreeNode treeNode = new TreeNode(input);
                    //Add our parent node
                    outputTree.Nodes.Add(treeNode);
                    //Add our child node
                    treeNode.Nodes.Add(ip);
                }
                else
                {
                    TreeNode[] treeNode = outputTree.Nodes.Find(input, true);
                    //Add only child node
                    foreach (var node in treeNode)
                    {
                        node.Nodes.Add(ip);
                    }
                }

                outputTree.EndUpdate();
            }
        }

【问题讨论】:

    标签: c# .net c#-4.0 treeview


    【解决方案1】:

    我能够让它工作。通过将包含数据的键动态添加到父节点,然后我可以使用该键找到父节点以将正确的子节点添加到它们。

    public void UpdateScan(string input, string ip)
    {
        lock (outputTree)
        {
            outputTree.BeginUpdate();
    
            if (! outputTree.Nodes.ContainsKey(input))
            {
                TreeNode treeNode = new TreeNode(input);
                treeNode.Name = input;
                //Add our parent node
                outputTree.Nodes.Add(treeNode);
                //Add our child node
                treeNode.Nodes.Add(ip);
            }
            else
            {
                TreeNode[] found = outputTree.Nodes.Find(input, true);
                TreeNode newChild = new TreeNode(ip);
                //Add only child node
                found[0].Nodes.Add(newChild);
            }
    
            outputTree.EndUpdate();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2019-08-28
      相关资源
      最近更新 更多