Progmatically Creating TreeNodes and Child Nodes [modified]
-
heres an idea... should do the job anyway...
void CreateNodes(string[] urls)
{
foreach(string url in urls)
{
string[] splits = url.Split('\\');TreeNode temp = GetNode(splits[0], TreeView.Nodes);
if(temp == null)
temp = TreeView.Nodes.Add(splits[0]);for(int i = 1; i < splits.Length; i++)
{
temp = GetNode(splits[i], temp.Nodes);
if(temp == null)
temp = temp.Nodes.Add(splits[i]);
}}
}
TreeNode GetNode(string text, TreeNodeCollections nodes)
{
foreach(TreeNode n in nodes)
if(n.Text == text)
return n;
return null;
}Life goes very fast. Tomorrow, today is already yesterday.
modified on Friday, March 27, 2009 12:47 PM
-
heres an idea... should do the job anyway...
void CreateNodes(string[] urls)
{
foreach(string url in urls)
{
string[] splits = url.Split('\\');TreeNode temp = GetNode(splits[0], TreeView.Nodes);
if(temp == null)
temp = TreeView.Nodes.Add(splits[0]);for(int i = 1; i < splits.Length; i++)
{
temp = GetNode(splits[i], temp.Nodes);
if(temp == null)
temp = temp.Nodes.Add(splits[i]);
}}
}
TreeNode GetNode(string text, TreeNodeCollections nodes)
{
foreach(TreeNode n in nodes)
if(n.Text == text)
return n;
return null;
}Life goes very fast. Tomorrow, today is already yesterday.
modified on Friday, March 27, 2009 12:47 PM
Looked very promising, but the code you sent was creating each url folder as a root node. It wasn’t breaking them down into child or grandchild nodes. ps i'm using c#.net framework 3.5 here's the code you sent... I had to modify it slightly. private void Form1_Load(object sender, EventArgs e) { TreeNode tn = new TreeNode(); treeView1.Nodes.Add(tn); string[] urls = new string[2]; urls[0] = "a\\b\\c\\d"; urls[1] = "a\\b\\e\\f"; CreateNodes(urls); } void CreateNodes(string[] urls) { foreach (string url in urls) { string[] splits = url.Split('\\'); TreeNode temp = GetNode(splits[0], treeView1.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[0]); for (int i = 1; i < splits.Length; i++) { TreeNode temp2 = GetNode(splits[i], temp.Nodes); if (temp2 == null) temp2 = treeView1.Nodes.Add(splits[i]); } } } TreeNode GetNode(string text, TreeNodeCollection nodes) { TreeNode result = null; foreach (TreeNode n in nodes) if (n.Text == text) { result = n; break; } return result; }
-
Looked very promising, but the code you sent was creating each url folder as a root node. It wasn’t breaking them down into child or grandchild nodes. ps i'm using c#.net framework 3.5 here's the code you sent... I had to modify it slightly. private void Form1_Load(object sender, EventArgs e) { TreeNode tn = new TreeNode(); treeView1.Nodes.Add(tn); string[] urls = new string[2]; urls[0] = "a\\b\\c\\d"; urls[1] = "a\\b\\e\\f"; CreateNodes(urls); } void CreateNodes(string[] urls) { foreach (string url in urls) { string[] splits = url.Split('\\'); TreeNode temp = GetNode(splits[0], treeView1.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[0]); for (int i = 1; i < splits.Length; i++) { TreeNode temp2 = GetNode(splits[i], temp.Nodes); if (temp2 == null) temp2 = treeView1.Nodes.Add(splits[i]); } } } TreeNode GetNode(string text, TreeNodeCollection nodes) { TreeNode result = null; foreach (TreeNode n in nodes) if (n.Text == text) { result = n; break; } return result; }
-
where you have put temp2 is wrong it was not meant to be another instance. see my other post for edited code
Life goes very fast. Tomorrow, today is already yesterday.
Sorry mate, just noticed that! :wtf: I've corrected temp2 as your right, it shouldn't be a new instance. I've re-tested the code but for some reason it's still populating the root nodes only??? I think i'll give it a rest for a bit and take another look later (with a fresh head); cos the code i'm looking at, to me should work. :~ :) void CreateNodes(string[] urls) { foreach (string url in urls) { string[] splits = url.Split('\\'); TreeNode temp = GetNode(splits[0], treeView1.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[0]); for (int i = 1; i < splits.Length; i++) { temp = GetNode(splits[i], temp.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[i]); } }
-
Sorry mate, just noticed that! :wtf: I've corrected temp2 as your right, it shouldn't be a new instance. I've re-tested the code but for some reason it's still populating the root nodes only??? I think i'll give it a rest for a bit and take another look later (with a fresh head); cos the code i'm looking at, to me should work. :~ :) void CreateNodes(string[] urls) { foreach (string url in urls) { string[] splits = url.Split('\\'); TreeNode temp = GetNode(splits[0], treeView1.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[0]); for (int i = 1; i < splits.Length; i++) { temp = GetNode(splits[i], temp.Nodes); if (temp == null) temp = treeView1.Nodes.Add(splits[i]); } }
-
-
heres an idea... should do the job anyway...
void CreateNodes(string[] urls)
{
foreach(string url in urls)
{
string[] splits = url.Split('\\');TreeNode temp = GetNode(splits[0], TreeView.Nodes);
if(temp == null)
temp = TreeView.Nodes.Add(splits[0]);for(int i = 1; i < splits.Length; i++)
{
temp = GetNode(splits[i], temp.Nodes);
if(temp == null)
temp = temp.Nodes.Add(splits[i]);
}}
}
TreeNode GetNode(string text, TreeNodeCollections nodes)
{
foreach(TreeNode n in nodes)
if(n.Text == text)
return n;
return null;
}Life goes very fast. Tomorrow, today is already yesterday.
modified on Friday, March 27, 2009 12:47 PM
I edited your code slightly. There was on error on the line:
temp = temp.Nodes.Add(splits[i]);
As temp was null so temp.Nodes was throwing an error.private void createNodes(string[] urls)
{
foreach (string url in urls)
{
string[] splits = url.Split('\\');if (splits.Length > 0) { TreeNode temp; if ((temp = GetNode(splits\[0\], this.treeView1.Nodes) == null) temp = this.treeView1.Nodes.Add(splits\[0\]); for (int i = 1; i < splits.Length; i++) { TreeNode temp2; if ((temp2 = GetNode(splits\[i\], temp.Nodes) == null) temp = temp.Nodes.Add(splits\[i\]); else temp = temp2; } } }
}
hmmm pie
modified on Tuesday, April 21, 2009 8:18 AM
-
Hi guys I’m at my wits end with TreeNodes. X| What i'm trying to do, to me is very simple. But for some reason I just can't get my head around it! :sigh: I’m currently working on a project where basically I’m splitting a Url address into a string array and creating each node and child node from the url segments (just like the Windows Explorer.) \\ this is the plan 1) step through each url string url1 = @"www.somedomain.com\home\about_us.aspx"; string url2 = @"www.somedomain.com\home\contact_us.aspx"; string url3 = @"www.somedomain.com\home\main.aspx"; 2) split the current url into sections 3) process the url sections and create new nodes and child nodes or merge nodes if needed 4) output results the final output I'm trying to get... + www.somedomain.com _+ home ___+ about_us.aspx ___+ contact_us.aspx ___+ main.aspx but in short i'm getting something like... + www.somedomain.com _+ home ___+ about_us.aspx ___+ contact_us.aspx ___+ main.aspx _+ home ___+ about_us.aspx ___+ contact_us.aspx ___+ main.aspx Looking at the code below, Is there another way around this problem or am I just being stupid??? Any help would be very much appreciated. Please feel free to rip the hell out of my coding as I've kind of lost the plot! Thanks again! :)
private void Form1_Load(object sender, EventArgs e)
{
TreeNode treeNode = new TreeNode();
treeView1.Nodes.Add(treeNode);
treeView1.PathSeparator = "\\"// Add first url address
BuildNodes(treeNode,@"1\2\3\4\5\6\7");// Add the next url address merging duplicate folders together...
BuildNodes(treeNode, @"1\2\4\5\6");
}private void BuildNodes(TreeNode treeNode, string address)
{
string[] add = SplitNodes(address);int max = add.Length; string key = String.Empty; TreeNode\[\] treeNodes = new TreeNode\[max\]; key = add\[0\]; treeNodes\[0\] = treeNode; treeNodes\[0\].Text = add\[0\]; treeNodes\[0\].Name = key; for (int i = 1; i < max; i++) { key = GetKey(key, add\[i\]); treeNodes\[i\] = new TreeNode(); treeNodes\[i\].Text = add\[i\]; treeNodes\[i\].Name = key; TreeNode\[\] tns = treeNodes\[i - 1\].Nodes.Find(treeNodes\[i\].Name, true); // if node exists skip it else add the new node if (tns.Length == 0)//if { treeNodes\[i - 1\].Nodes.Add(treeNodes\[i\]); } }
}
private string GetKey(string key, string v
Given the nature of trees in general, you might want to simplify things and go for a recursive approach. Something like this (pseudo-code):
InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0)
}InsertURL(TreeNode parent, string[] urlParts, int index)
{
TreeNode nextChild = parent.Find(urlParts[index])If nextChild is not valid
{
Create a new node under parent corresponding to urlParts[index]
nextChild = The new node
}if ++index < urlParts.Length
{
InsertURL(nextChild, urlParts, index)
}
}(Yes, I know the syntax isn't proper C#. That's intentional, so ya don't try to copy-paste and compile it :) ) Notes: 1) The array gets passed as a reference, so it's faster to just send the whole array to each recursive call instead of stripping out each part as we place it. 2) Note the ++ in the last comparison. Increment it, THEN see if the new value is in-bounds, then recurse if valid.
-
Given the nature of trees in general, you might want to simplify things and go for a recursive approach. Something like this (pseudo-code):
InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0)
}InsertURL(TreeNode parent, string[] urlParts, int index)
{
TreeNode nextChild = parent.Find(urlParts[index])If nextChild is not valid
{
Create a new node under parent corresponding to urlParts[index]
nextChild = The new node
}if ++index < urlParts.Length
{
InsertURL(nextChild, urlParts, index)
}
}(Yes, I know the syntax isn't proper C#. That's intentional, so ya don't try to copy-paste and compile it :) ) Notes: 1) The array gets passed as a reference, so it's faster to just send the whole array to each recursive call instead of stripping out each part as we place it. 2) Note the ++ in the last comparison. Increment it, THEN see if the new value is in-bounds, then recurse if valid.
Don’t worry about the syntax Ian. Your pseudo-code’s pretty clear and straight forward. I’m just coding the methods out now. Fingers cross this might be the one! :-D I don’t know if anyone else has had this problem? I think I’d better post it up once we’ve got it working. Thanks so much for your help, really appreciate it :thumbsup:
-
Given the nature of trees in general, you might want to simplify things and go for a recursive approach. Something like this (pseudo-code):
InsertURL(TreeNode root, string url)
{
InsertURL(root, SplitNodes(url), 0)
}InsertURL(TreeNode parent, string[] urlParts, int index)
{
TreeNode nextChild = parent.Find(urlParts[index])If nextChild is not valid
{
Create a new node under parent corresponding to urlParts[index]
nextChild = The new node
}if ++index < urlParts.Length
{
InsertURL(nextChild, urlParts, index)
}
}(Yes, I know the syntax isn't proper C#. That's intentional, so ya don't try to copy-paste and compile it :) ) Notes: 1) The array gets passed as a reference, so it's faster to just send the whole array to each recursive call instead of stripping out each part as we place it. 2) Note the ++ in the last comparison. Increment it, THEN see if the new value is in-bounds, then recurse if valid.
Big, big thank you musefan and Ian! I’ve now got it working brilliantly. Based on the pseudo-code Ian posted (which worked great after tweeking!) I’ve decided to post the working code incase anyone else out there is experiencing TreeNode’s from hell lol. "ps, i'm not great with codeproject, so if you know of a better place for me to post this answer, please let me know." Well here’s the working code… :-D :thumbsup: Add a TreeView control to Form1 and name it treeView1. and then add this code..
private void Form1_Load(object sender, EventArgs e) { TreeNode tn = new TreeNode("Root"); // set PathSeparator to url paths treeView1.PathSeparator = "/"; // Create any old folders and process InsertURL(tn, @"mywebsite.com/home/about_us.aspx"); InsertURL(tn, @"mywebsite.com/home/contact_us.aspx"); InsertURL(tn, @"mywebsite.com/home/main.aspx"); InsertURL(tn, @"mywebsite.com/blogs/seo.aspx"); InsertURL(tn, @"mywebsite.com/blogs/linkchecks.aspx"); InsertURL(tn, @"mywebsite.com/blogs/howto/gettheresults.aspx"); //add nodes to treeView treeView1.Nodes.Add(tn); } private void InsertURL(TreeNode root, string url) { InsertURL(root, SplitNodes(url), 0); } private string[] SplitNodes(string nodeString) { char[] delimiter = treeView1.PathSeparator.ToCharArray(); return nodeString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); } private void InsertURL(TreeNode parent, string[] urlParts, int index) { // Create the node if it doesn't exist. CreateNode(parent, urlParts[index]); // get the next urlPart index++; if (index < urlParts.Length) { InsertURL(parent.Nodes[urlParts[index-1]], urlParts, index); } } private static void CreateNode(TreeNode treeNode, string name) { // Add the node if it don't exist if (treeNode.Nodes[name] == null) { treeNode.Nodes.Add(name, name); } }
modified on Saturday, March 28, 2009 5:57 AM