Question Building a tree using the data from txt file

-SKY_

New member
Joined
Jan 15, 2018
Messages
2
Programming Experience
Beginner
Hi. I have a Homework, practically 100% similar to the one found here (LINK)

Code goes like this..

C#:
private void Form1_Load(object sender, EventArgs e)
        {
            List<TreeNode> allNodes = new List<TreeNode>();
            using (StreamReader sr = new StreamReader("NodeList.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    Regex regex = new Regex(@"(\w.*?)\((.*?)\)");
                    Match match = regex.Match(str);
                    if (match.Success)
                    {
                        string nodeText = match.Groups[1].Value;
                        string nodeId_parentId = match.Groups[2].Value;
                        string nodeId = nodeId_parentId.Contains(',') ? nodeId_parentId.Split(',')[0] : nodeId_parentId;
                        string parentId = nodeId_parentId.Contains(',') ? nodeId_parentId.Split(',')[1] : null;

                        TreeNode node = new TreeNode();
                        node.Text = nodeText; //NodeText
                        node.ToolTipText = nodeId; //As TreeNode class  doesn't contain a property named NodeId, I store the id with this  property
                        node.Tag = parentId; //ParentId
                        allNodes.Add(node);
                    }
                }
            }

            TreeNode currentNode = allNodes.FindAll(n => n.Tag == null).FirstOrDefault();
            AppendChildNodes(currentNode, allNodes);
            this.treeView1.Nodes.Add(currentNode);
        }

        private void AppendChildNodes(TreeNode currentNode,List<TreeNode> allNodes)
        {
            currentNode.Nodes.AddRange(allNodes.FindAll(n =>  (n.Tag!=null) && (n.Tag.ToString() ==  currentNode.ToolTipText)).ToArray());
            foreach (TreeNode node in currentNode.Nodes)
            {
                AppendChildNodes(node, allNodes);
            }
        }



I copy/pasted the code. However, when I try to start/compile the code Proposed as answer by Cor Ligthert

I get an error : "error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'"


I did name the Text file NodeList.txt, (same as given in code).

"NodeList.txt"

But I guess I am missing something noobish here, as this is the first time I am working with C# at all.
 
When people post code examples, it's likely to only be code relevant to the specific issue. There is more code than that required to make a project run but people aren't going to waste their time and everyone else's posting that every time. You need to learn the basics of C# for yourself and put that knowledge to use rather than expecting others to provide a turnkey solution for you to copy and paste each time. For one thing, that code has a method that is clearly supposed to be handling the Load event of Form1. Based on your error message, you didn't put it inside the definition for a form named Form1 so of course it won't work. Even if you did that it still wouldn't work because that method wouldn't be registered as an event handler. You need to read the code and understand what it's doing at a basic level at least first. Create a Windows Forms project and add the required TreeView and then create a handler for the Load event. You can then copy the contents of the Load event handler from the example into your own project. Etc.
 
Thanks,, I have just started with C# like 47 hours ago. I Am going through some tutorials as of now. If you have an idea for any tutorial which would be helpful here,, please do post a link. Thanks.
 
There's a tutorial link in my signature below. I've not really looked too closely at it but the VB.NET tutorial at the same site is one of the most highly-regarded for beginners.
 
Back
Top Bottom