2014-04-25 1 views
0

treeview를 업데이트하기 위해 threadsafe 콜백으로 사용되는 메서드가 있습니다. 두 개의 문자열 매개 변수가 필요합니다. 첫 번째는 전달 된 데이터이고 두 번째는 확인한 호스트의 IP입니다.중복 treeview 노드를 감지하고이를 자식으로 올바르게 할당 할 수 없습니다.

현재 트리보기에 입력 문자열이 들어있는 문자열이 있는지 확인하려고하고 있는데, 트리 뷰에 부모 노드로 추가해야하는 것이 아닌 경우 아래에 ip 문자열을 추가하십시오 아이. 부모 노드로 입력 문자열을 이미 포함하고있는 경우에는 데이터 문자열이 일치하는 부모 노드 아래에 IP 주소 만 추가해야합니다. 그래서 기본적으로 ips를 정렬합니다. 각 상위 노드에는 여러 개의 ips가 있습니다.

내 문제는 내 메서드는 각 문자열을 자신이 부모인지 여부에 관계없이 추가한다는 것입니다. 또한 부모가 중복 입력의 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(); 
      } 
     } 

답변

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(); 
    } 
} 
관련 문제