Files within TreeView

gladiator83x

Member
Joined
Jan 15, 2008
Messages
11
Programming Experience
Beginner
Hi All,

I have six directories which all contain files within them. I am trying to make a treeview that will display them in hierarchal form. The directory itself is the root, and the files are the children. I was doing well with displaying the roots (6 directories) but I am running into problems with trying to set the files to be the children nodes. Here is my code:
*---------------------------------------------------------*
Dim dir1 As New IO.DirectoryInfo("1.0 Test Objective")
Dim dir2 As New IO.DirectoryInfo("2.0 Test Setup")
Dim dir3 As New IO.DirectoryInfo("3.0 Test Status")
Dim dir4 As New IO.DirectoryInfo("4.0 Test Methods")
Dim dir5 As New IO.DirectoryInfo("5.0 Test Results")
Dim dir6 As New IO.DirectoryInfo("6.0 Test Planning and Goals")


'---------------SECTION 1.0-----------------
Dim d1 As IO.FileInfo() = dir1.GetFiles()
Dim directory1 As IO.FileInfo
Dim node1 As TreeNode = TreeView1.Nodes.Add(dir1.Name)

node1.Nodes.Add(dir1.Name)

For Each directory1 In d1
node1.Nodes.Add(directory1)
Next


'--------------------------------------------

I do the same thing for the next 5 directories. I think that I need to make node1 an array somehow b/c there's more than one file in that directory. I tried things such as:

Dim node5() As TreeNode = TreeView1.Nodes.Add(dir5.Name)

but I was unsuccessful. Any help would be greatly appreciated. Thanks!
 
You seem to be doing great, so what is actually your question?
 
--------------------------------------------------------------------------------

Hi John,

My question is how can I display the list of files as child nodes under the root (directory) node? I was leaning on towards using an array of some sort. I get an error b/c of the following lines of code.

Dim node1() As TreeNode = TreeView1.Nodes.Add(dir1.Name)

For Each directory1 In d1
node1.Nodes.Add(directory1)
Next


To be a little more clearer, I think that I am decalring something wrong, and I was wondering how I could display a set of files (set of child nodes) under that one root (directory) node?
 
You have done something wrong with the declaration of node1 here (array), this was fine in your first post.
 
My code in the first post generates an error that states:

Overload resolution failed because no accessible 'Add' can be called with these arguments:

'Public Overridable Function Add(node As System.Windows.Forms.TreeNode) As Integer': Value of type 'System.IO.FileInfo' cannot be converted to 'System.Windows.Forms.TreeNode'. 'Public Overridable Function Add(text As String) As System.Windows.Forms.TreeNode': Value of type 'System.IO.FileInfo' cannot be converted to 'String'.C:\Documents and Settings\cr\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 114 13 windowsApplication1


The line that it referring to is:

node1.Nodes.Add(directory1)

I believe that I am trying to make multiple files within the root node1, instead of just one file being called node 1; that is why I was wondering if I could use an array of some sort to make this error disappear?
 
Oh, I didn't notice that. Perhaps you want to add the Name string just like you did with the directory (dir1)?
"Directory1" is a strange variable name for a FileInfo object, don't you think?
Here is a little code that should save you a big time:
VB.NET:
Expand Collapse Copy
Dim folders As New List(Of String)
folders.Add("C:\Temp\dir1")
folders.Add("C:\Temp\dir2")
For Each folder As String In folders
    Dim dir As New IO.DirectoryInfo(folder)
    Dim node As TreeNode = Me.TreeView1.Nodes.Add(dir.Name)
    For Each file As IO.FileInfo In dir.GetFiles
        node.Nodes.Add(file.Name)
    Next
Next
 
Back
Top