none
Building a tree using the data from txt file /repeat.

    Question

  • Hi. I have a Homework, practically 100% similar to the one found  here (Link missing as THey need to verify my account, please do so I can post a link.)

    Code goes like this

    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.



    • Edited by _Sky_G Monday, January 15, 2018 9:38 PM
    • Moved by CoolDadTxMVP Tuesday, January 16, 2018 2:53 PM Winforms related
    Monday, January 15, 2018 8:26 PM

All replies

  • Hello,

     First place you code in the code block when you create a posting, helps readability.

    Second, remember that Namespace scope is limited to class, struct, delegate, etc.

    Somewhere in your code you have defined a void method outside of class Form1 scope.

     

     Hope this helps :)

    Monday, January 15, 2018 9:33 PM
  • This is the link for the task I am trying to do.  https://social.msdn.microsoft.com/Forums/en-US/b5a8750c-b955-4e05-806b-611f89d02fb6/i-want-to-generate-a-tree-view-using-text-file-in-c-but-i-am-unable-to-apply-the-concept-would?referrer=http://social.msdn.microsoft.com/Forums/en-US/b5a8750c-b955-4e05-806b-611f89d02fb6/i-want-to-generate-a-tree-view-using-text-file-in-c-but-i-am-unable-to-apply-the-concept-would?referrer=http://social.msdn.microsoft.com/Forums/en-US/b5a8750c-b955-4e05-806b-611f89d02fb6/i-want-to-generate-a-tree-view-using-text-file-in-c-but-i-am-unable-to-apply-the-concept-would?referrer=http://social.msdn.microsoft.com/Forums/en-US/b5a8750c-b955-4e05-806b-611f89d02fb6/i-want-to-generate-a-tree-view-using-text-file-in-c-but-i-am-unable-to-apply-the-concept-would?forum=csharpgeneral

    • Edited by _Sky_G Monday, January 15, 2018 9:45 PM
    Monday, January 15, 2018 9:45 PM
  • Hello,

     Depending of the complexity of the data, any Tree can be coded. All you need

    to do is declare a RootNode. Then read data and identify the data is info or

    is subNode. Think of it like the Directory structure of a hard drive.  If you can

    provide the data you want to read into a Tree then I can provide more help.

     Hope this helps :)

    Tuesday, January 16, 2018 12:08 AM
  • I wrote a blog post last month about building a TreeView: https://geek-goddess-bonnie.blogspot.com/2017/12/putting-up-tree.html

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, January 16, 2018 2:05 AM
  • Hello _SKy_G,

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

    The error message indicates that you locate the copy code at the bad positions. Maybe some methods are outside of the class declaration. If so, you must place it inside the class. Please check your braces when you copy/paste the code into you project.

    And also I tested the code from the link, it works well on my side.

    And you could get started with windows form here.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/getting-started-with-windows-forms

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 16, 2018 6:02 AM
  • I had tried to compile the code. It did not work. Could you post a few screen shots, so I might get an idea of what I am doing wrong
    Saturday, February 3, 2018 4:28 PM
  • Which of us are you replying to? Which code are you referring to that wouldn't work?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, February 3, 2018 5:11 PM
  • Well FEI HU was able to compile it. But I am still not able to do so. He posted the screenshot of his result. I would beg if anyone could post a screenshot of the FULL code,, like everything from the (using System;) to the last bracket.  .. As I do not get it why I am not able to compile it. .. Sorry for being suhc a noob
    • Edited by _Sky_G Wednesday, February 7, 2018 7:16 PM
    Wednesday, February 7, 2018 7:10 PM
  • Hi Sky,

    I have a better idea ... why don't you post the FULL code that you have and perhaps we can see where you went wrong. Use the "Insert Code Block" button for posting code. It's the button to the right of the "HTML", the one that has two brackets "< >"


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, February 8, 2018 2:32 AM
  • using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System;
    
    namespace Tree
    {
    
    	class MainClass
    	{
    	
    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);
                }
            }
    
    
    	}
    
    }
    

    <treenode><treenode><treenode></treenode></treenode></treenode>
    Thursday, February 8, 2018 10:59 AM
  • Hi Sky,

    Yeah, that won't compile like that. That's because the Form1_Load() and the AppendChildNodes() methods code should be part of an existing Form that you already have. In this case, it's in a Form class called Form1. And Form1 has a TreeView on it called treeView1. At minimum, you need to have created a Form (doesn't have to be called Form1, it could be whatever you want to call it), dragged a TreeView onto the Form, and created a Load() handler (by going to the Properties panel while in the Form1 Designer, go to the Load event there). This much you should have learned in class.

    Then, simply copy/paste this portion of that code into your own FormWhatever_Load:

    private void FormWhatever_Load(object sender, EventArgs e) // this line you should already have
    {
        // This goes in your Form's Load event handler method
        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);
    }
    

    And add this method anywhere else in your Form's code (you can put it before or after the Load method, it doesn't matter):

    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);
        }
    }
    
    Hope that makes it a little clearer!


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, February 8, 2018 3:14 PM