【发布时间】: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();
}
}
【问题讨论】: