Hello! There is a task to fill the TreeView tree from the text file of the tree structure (nesting is determined by the number of tabs at the beginning of each line). Wrote a class:
public class Tree { private List<TreeBranch> Child { get; set; } private void Fill(StreamReader stream) { bool isEmpty = true; Child = new List<TreeBranch>(); TreeBranch treeBranch = new TreeBranch(); treeBranch.Child = new List<dynamic>(); while (!stream.EndOfStream) { string node = stream.ReadLine(); int countTabs = GetCountTabs(node); if (!isEmpty && countTabs == 0) { Child.Add(treeBranch); treeBranch = new TreeBranch(); treeBranch.Child = new List<dynamic>(); treeBranch.Root = node; continue; } else if (isEmpty && countTabs == 0) { treeBranch.Root = node; } else { treeBranch.Child.Add(new { Name = node, Level = countTabs }); } isEmpty = false; } Child.Add(treeBranch); } private int GetCountTabs(string node) { return node.Split(new string[] { "\t" }, StringSplitOptions.None).Count() - 1; } public void SetTreeView(StreamReader stream, TreeView treeView) { Fill(stream); foreach (var branch in Child) { TreeNode treeNode = new TreeNode(branch.Root); AddChildren(treeNode, branch.Child); treeView.Nodes.Add(treeNode); } } private void AddChildren(TreeNode treeNode, List<dynamic> child) { foreach (var item in child) { TreeNode newNode = new TreeNode(); treeNode.Nodes.Add(item.Name); //здесь нужно применить рекурсию, в этом моя проблема AddChildren(newNode, (from node in child where item.Level - node.Level == 1 select node).ToList()); } } }
The Fill method (StreamReader stream) generates a List <TreeBranch>, each object of which has a root Root parent and a list of all other descendants, each descendant has a Name and Level (nesting level). TreeBranch class:
public class TreeBranch { public string Root { get; set; } public List<dynamic> Child { get; set; } }
Well, actually on the form there is a button and a tree. Button event handler:
private void button1_Click(object sender, EventArgs e) { StreamReader fileSteram= new StreamReader("tree1.txt"); Tree tree = new Tree(); tree.SetTreeView(fileSteram, treeView1); }
In the SetTreeView method (StreamReader stream, TreeView treeView) you need to call the recursive AddChildren method. There is a problem in it .. Help solve, please, who is not difficult .. I can not use recursion .. urgently need .. Thank you!
An example of a text file of 2 objects of type TreeBranch:
1living things plants animals inverterbrates verterbrates birds flying birds land birds water birds fish amphibians reptiles mammals 2living things plants animals inverterbrates verterbrates birds flying birds land birds water birds fish amphibians reptiles mammals
[Updated]
private void AddChildren(TreeNode treeNode, List<dynamic> child) { var newchild = (from node in child where node.Parent == treeNode.Text select node).ToList(); foreach (var item in newchild) { TreeNode newNode = new TreeNode(item.Name); treeNode.Nodes.Add(newNode); AddChildren(newNode, child); } }