Getting a directory structure into Einhugur Treeview

I need help. Usually I see very easily what to do, but I have been working on this all day and do not get anywhere.

I want to use this nice piece of code posted by Tom Iwaniec in
to fill Einhugur Treeview.

Sub ListFiles(theFolder as FolderItem) if theFolder<>nil then if theFolder.Directory then for i as Integer=1 to theFolder.Count if theFolder.Item(i).Directory then ListFiles(theFolder.Item(i)) else listbox1.addrow theFolder.Item(i).name end if next end if end if End Sub

I have been looking at this in the demo project Treeview 2 columns exampler :

[code] me.AppendNode(new TreeViewNode(“Test1”,DocumentIconNormal))
me.AppendNode(new TreeViewNode(“Test2”,DocumentIconNormal))
me.AppendNode(new TreeViewNode(“Test3”,DocumentIconNormal))

node = new TreeViewNode(“Test4”,FolderIconNormal)
node.AppendNode(new TreeViewNode(“Test4a”,DocumentIconNormal))

node2 = new TreeViewNode(“Test4b”,FolderIconNormal)
node2.AppendNode(new TreeViewNode(“Test4b - 1”,FolderIconNormal))
node2.AppendNode(new TreeViewNode(“Test4b - 2”,FolderIconNormal))
node2.AddSubItem(“Some Text”)


node.AppendNode(new TreeViewNode(“Test4c”,DocumentIconNormal))
node.AppendNode(new TreeViewNode(“Test4d”,DocumentIconNormal))
node.AppendNode(new TreeViewNode(“Test4e”,DocumentIconNormal))
node.AppendNode(new TreeViewNode(“Test4f”,DocumentIconNormal))
node.AddSubItem(“More test”)



This creates a nice embedded structure with folders that can be expanded or folded.

I guess I am stuck because the recursion is done by the method above by calling itself. I do not see very well how to append the node when it had got to the last level of folder. I could create cascading methods, but it would be awfully cumbersome and limited in number of levels, whereas that elegant method does unlimited levels in very few lines.

I will very much appreciate ideas to spot the last level of imbrication. Or any advice for that matter.


Recursion is always strange for human brains. Let me have a look at my working code in the morning.

Much appreciated. At the moment the only way I can think of is to gather all the paths in an array, and process them afterward. But yet, I am stuck with figuring out the parent/child relationships to form the trees.

If Beatrix already has code , then this is probably not as useful - but it sounds like the first set of code only lists files, and does not ‘indent’ them.
To make the indentation happen, you need to add nodes to other nodes

When you start, you will have one node to represent the top folder.
Files are added to that node.
Directories add a new folder row, and then fill those rows

So (untested - I dont have this control), I think you need to add the concept of ParentNode and ChildNode

dim ParentNode as TreeViewNode

Sub ListFiles(theFolder as FolderItem)
dim newParent as TreeViewNode
dim oldParent as TreeViewNode
oldParent = ParentNode
if theFolder<>nil then

if theFolder.Directory then

  //first time in, ParentNode is the top folder
 if Parentnode = nil then
      newParent = new TreeViewNode(,FolderIconNormal)
     Parentnode = newParent
     //attach a folder to the previous folder
     newParent =  ParentNode.AppendNode(new TreeViewNode(,FolderIconNormal))
 end if

  for i as Integer=1 to theFolder.Count
    if theFolder.Item(i).Directory then

//found a folder so start a new branch
ParentNode = newParent
//add a normal row to the current parent
newParent.AppendNode(new TreeViewNode(theFolder.Item(i).name, FolderIconNormal))

    end if
end if

end if
//reset the parent record
ParentNode = OldParent
End Sub[/code]

Hi Jeff. Precisely my problem was to incorporate the treeview building into the ListFiles method.

Thank you.

Hi Michel,

The code below shows the structure of the directory in your code with a few lines of me.
Directories are shown as "DIR: " with a number of dashes in front which show the indent level. If you run this on a Windows pc with a deeper structure you can see that Windows doesn’t read first all directories and then the files, but it can be a directory followed by the included files and/or directories. Just fun to see :wink:

static s as string = “”
if theFolder<>nil then
if theFolder.Directory then
s=s + “–”
for i as Integer=1 to theFolder.Count
if theFolder.Item(i).Directory then
s=s + “–”
Listbox1.AddRow s+"DIR: "+theFolder.Item(i).Name
listbox1.addrow s+theFolder.Item(i).name
end if
end if
end if

Thank you Andre.

OK. I must have been awfully tired for a couple day. I took a nap, and as usual, everything became clear.

[code]Function addChildNodes(Extends N as TreeViewNode, theFolder as FolderItem) As TreeViewNode
dim item as FolderItem
dim node as TreeViewNode
if theFolder.Count <= 0 then return N

for i as Integer = 1 to theFolder.count
item = theFolder.Item(i)
if item.Directory then
node = new TreeViewNode(,folderIconNormal)
node.ItemData = item
end if

return N
End Function

I need only folders, but it would be rather easy to add documents.

Thank you again for the suggestions.