Ferrari, Eduardo wrote:
Hi Everybody
I have a string that contains part of a directory structure that will
be created in the future. I want to display this future tree on a
treeview for the user see and then approve it.
I was able to populate the first level of the tree, but I'll need
some information to how populate the child levels of the tree.
For example, if I have the directory (not created on the OS yet):
Dir1
Dir1/Dir2
Dir1/Dir2/Dir3
Now I'm using:
iniSlash = path.IndexOf(@"\");
if(iniSlash != -1)
{
return path.Substring(0, iniSlash);
}
So I get the first directory (Dir1) and I add it to the tree:
TreeNode ChildNode = new TreeNode(rootNode);
aNode.Nodes.Add(ChildNode);
Where the rootNode = path (returned from the string).
How can I select the populated nodes and get the child for each node
on the tree?
Someone has something like this ?
Use recursion.
First split the path on '\'. This will give you all folders in an
array:
string[] folders = path.Split('\');
then create a hashtable which stores path to treenode combinations.
So if you have the path c:\foo\bar\bar2, it will result in 3 nodes:
c:\foo, bar and bar2 and you store these 3 nodes as:
c:\foo
c:\foo\bar
c:\foo\bar\bar2
in the hashtable, with the full path to their folder as key and the
treenode as value
then create a small routine which in a loop from front to back renders
a path in the treeview. That routine first splits the folders as above,
then walks from front to back through the array and builds a relative
path from the parent path. So in our example:
c:\foo doesn't have a parent path, as it's the first element in the
array. bar is a folder which has c:\foo as parent path. Grab the node
from the hashtable and add a subnode to that node with the text 'bar',
and store the node under c:\foo\bar in the hashtable.
etc.
FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------