2016-06-14 2 views
2

나는 이와 같은 탭 형식의 텍스트로 트 리뷰 구조를 내 보내야 탭 형식의 텍스트로 표시 트리 구조 :C#을 윈폼 -

node 1 
    child node 1.1 
     child node 1.1.1 
     child node 1.1.1.1 
node 2 
    child node 2.1 
     child node 2.1.1 
     child node 2.1.1.1 
...etc 
나는 다음과 같은 재귀 루틴 생성

:

 public static string ExportTreeNode(TreeNodeCollection treeNodes) 
    { 
     string retText = null; 

     if (treeNodes.Count == 0) return null; 

     foreach (TreeNode node in treeNodes) 
     { 
      retText += node.Text + "\n"; 

      // Recursively check the children of each node in the nodes collection. 
      retText += "\t" + ExportTreeNode(node.Nodes); 
     } 
     return retText; 
    } 

그 일을 할 것이라고 기대하지만 그렇게하지는 않습니다. 대신 다음과 같이 트리 구조를 출력합니다.

node 1 
    child node 1.1 
    child node 1.1.1 
    child node 1.1.1.1 
    node 2 
    child node 2.1 
    child node 2.1.1 
    child node 2.1.1.1 

누군가가 도와 줄 수 있습니까? 많은 감사합니다!

+0

을 변경 그리고 이것은 내가 그것을 호출하는 방법은 다음과 같습니다 : 함수에 들여 쓰기 매개 변수를 추가 textTree = TreeViewSearch.ExportTreeNode (tvSearchResults.Nodes)) ; – user2430797

답변

1

이 행에 대한 가정은 올바르지 않습니다. 첫 번째 자식 노드 만 들여 쓰기 만합니다.

retText += "\t" + ExportTreeNode(node.Nodes); 

또한 탭은 집계되지 않습니다.

public static string ExportTreeNode(TreeNodeCollection treeNodes, string indent = "") 

retText += node.Text + "\n"; 

// Recursively check the children of each node in the nodes collection. 
retText += "\t" + ExportTreeNode(node.Nodes); 

retText += indent + node.Text + "\n"; 

// Recursively check the children of each node in the nodes collection. 
retText += ExportTreeNode(node.Nodes, indent + "\t"); 
+0

감사합니다. 그것은 내 문제를 해결했다. – user2430797