Share via


I want to generate a tree view using text file in c#. But I am unable to apply the concept. Would you please tell me how to do it?

Question

Monday, October 20, 2014 9:42 AM

The text file contains data in the following format
 
ABC(0)      //This is the root node 
XYZ(1,0)    //First digit in the brackets represent sub nodes and second digit represent 
              //  to which node it is to be attached
UVW(2,1)  
LMN(3,1) 
HKL(4,1)
aa(5,2)
bb(6,3)
cc(7,2)
And the coding for the tree view should follow the following procedure: 
1) Create a tree node variable.
2) Create a file stream reader.
3) While the file does not reach end, read line
4) Substring the three parts, that is, text on node, id of node, parent of node
5) node = new node (allot space) 
6) node.text = text part read in line   
    node.id = id part read in line 
7) if parent node=""    
    In case of no,find the node in treeview by id, attach this node as child of found  node      
    In case of yes, add it as root node in treeview 
   Then direct both to the (3) point/step.

All replies (5)

Tuesday, October 21, 2014 7:20 AM âś…Answered | 1 vote

Hello Medhavi,

Sounds like a homework. I think you need to learn how to use TreeView control in Windows Forms Application, and use some algorithm to recursively add those TreeNodes into the TreeView control. I just made a sample code here.

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);
            }
        }

This is the result:

I used a regular pattern to match the node text/node id/parent id in the text file, it's simple, similar to splitting the string with the SubString method.

As the built-in TreeNode class doesn't have properties named NodeId or ParentId, I used ToolTipText and Tag instead. Another approach is to create your custom node class which is derived from TreeNode, use this custom class to store NodeId and ParentId property.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Monday, October 20, 2014 5:16 PM

Before you can use a Treeview with a textfile, you have to create a kind of node system. For instance a collection of nodes.

That is not a question for the treeview itself but straight C# in your case. I advise you to ask that in the C# forum. 

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral

If you have that collection of nodes it is a piece of cake to set that in a treeview

Success
Cor


Monday, October 20, 2014 5:39 PM

thanks......

but if you please provide me with the code for the treeview it would prove beneficial for me.


Monday, October 20, 2014 5:59 PM

Done in the C# forum using a console application. 

Success
Cor


Tuesday, October 21, 2014 10:05 AM

Thanks a lot....now i am able to understand this.