0

ユーザーがドラッグして子ノードなどを作成できるツリービューにいくつかのノードがあります。

親ノードリストを取得するためにいくつかの方法を使用しています:

private static IList<Node> BuildParentNodeList(AdvTree treeView)
    {
        IList<Node> nodesWithChildren = new List<Node>();

        foreach (Node node in treeView.Nodes)
            AddParentNodes(nodesWithChildren, node);

        return nodesWithChildren;
    }

    private static void AddParentNodes(IList<Node> nodesWithChildren, Node parentNode)
    {
        if (parentNode.Nodes.Count > 0)
        {
            nodesWithChildren.Add(parentNode);
            foreach (Node node in parentNode.Nodes)

                AddParentNodes(nodesWithChildren, node);
        }
    }

次に、親ノードで拡張メソッドを使用して、すべての子孫ノードを取得します。

public static IEnumerable<Node> DescendantNodes(this  Node input)
{
        foreach (Node node in input.Nodes)
        {
            yield return node;
            foreach (var subnode in node.DescendantNodes())
                yield return subnode;
        }
    }

私のノードの典型的な配置は次のとおりです。

Computer
  Drive F
    Movies     
    Music
      Enrique
      Michael Jackson
        Videos

子ノードを持つすべてのノードのパスの文字列表現が必要です。例えば:

Computer\DriveF
Computer\DriveF\Movies\
Computer\DriveF\Music\
Computer\DriveF\Music\Enrique
Computer\DriveF\Music\Michael Jackson
Computer\DriveF\Music\Michael Jackson\Videos

上記の方法を使用してこの正確な表現を取得するのに問題があります。どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

1

これは私のために働いた:

private void button1_Click(object sender, EventArgs e)
{
  List<string> listPath = new List<string>();
  GetAllPaths(treeView1.Nodes[0], listPath);

  StringBuilder sb = new StringBuilder();
  foreach (string item in listPath)
    sb.AppendLine(item);

  MessageBox.Show(sb.ToString());
}

private void GetAllPaths(TreeNode startNode, List<string> listPath)
{
  listPath.Add(startNode.FullPath);

  foreach (TreeNode tn in startNode.Nodes)
    GetAllPaths(tn, listPath);
}
于 2011-09-24T19:18:58.540 に答える