Tree-view hierarchie

.NET 2.0 using VS2005

I'm trying to get this tree-view working properly.

Here's the directory structure on my c: in a specific folder


- Active
- Archive
|
|-- 04-05
|-- 05-06
|-- 06-07


Here's my code right now.


For Each _d As String In Directory.GetDirectories("C:\Change Request\")

Dim _folder As String = _d.Substring(_d.LastIndexOf("\") + 1)
tvListFolders.Nodes.Add(_folder)

Dim ThisDirectory As String = _folder

For Each _dd As String In Directory.GetDirectories("C:\Change Request\" & ThisDirectory)

Dim _dfolder As String = _dd.Substring(_dd.LastIndexOf("\") + 1)
tvListFolders.Nodes.Add(_dfolder)

Next

Next

And this is what display (see attachment) ... Now ... how do I get the 04-05, 05-06 and 06-07 folder moved under the Archived folder? instead of under "root"?

I would've thought the second for loop in there would give me that but .... obviously that didn't work. I think I need to assign something as parent but ... I'm new at .NET so ... explanation and help would be greatly appreciated.

ThanksOk, there are a few problems for a start the Add() function takes a node as an input not a string. What you want to be doing is adding a child node. I dont know VB.NET so i wrote it in C# and then converted it so i cant gauruntee the code is perfect but this shows the idea:


For Each _d As String In Directory.GetDirectories("C:\Change Request\")
Dim _folder As String = _d.Substring(_d.LastIndexOf("\") + 1)
Dim parentNode As TreeNode = New TreeNode(_folder)
For Each _dd As String In Directory.GetDirectories("C:\Change Request\" + _folder)
Dim _dfolder As String = _dd.Substring(_dd.LastIndexOf("\") + 1)
Dim childNode As TreeNode = New TreeNode(_dfolder)
parentNode.ChildNodes.Add(childNode)
Next
tvListFolders.Nodes.Add(parentNode)
Next


The trick is to add the child nodes to the parent nodes before you add it to the tree view, this way you dont have to bother with trying to find the node within the tree view. (Also there is no point declaring string vars for no reason as it just wastes memory).

Hope that helps, i haven't tested the code but it should work.

N.B.
I have found it is quite useful (not sure if its the best way in general though) to generate XML on the fly for the XMLDataSource and then bind the tree view to that. The XML string can be easily cached either into a file or in the cache. Its also pretty useful as .Net handles xml really well.Hey ... thanks for replying.

When I paste this into VS2005, I get a blue squiggly line under

parentNode.ChildNodes.Add(childNode)

When I hover I get this message: 'ChildNodes' is not a member of 'System.Windows.Forms.TreeNode'.

Any Idea?Nevermind ... I change that

parentNode.ChildNodes.Add(childNode)

to

parentNode.Nodes.Add(childNode)

and it works :)

Thanks for the help!Sorry i should have tested the code. Glad you got it working though. Working with objects gets a bit confusing at first, espescially passing them as parameters to functions, but soon you realise just how powerful a tool it is.

I think you use childnode if you are accesing the node via TreeView.Nodes[n].ChildNodes, although i cant remember tbh.
 
Back
Top