2016-06-14 16 views
2

ように私はこのようなタブ形式のテキストにツリービュー構造をエクスポートする必要があります。のC#のWinForms - 表示ツリー構造タブ形式のテキスト

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