Hi. I have a Homework, practically 100% similar to the one found here (LINK)
Code goes like this..
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.
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.