Notifications
Clear all

[Closed] Building a Directory Struct for dotNet TreeView

I’m struggling to work out how the hell to do this… I’ve got PEN’s code for a treeview, and I’ve found a directory struct function but can’t get them working together…Not sure I entirely understand what’s happening with structs yet, not found anything that’s explaining it well to me.

Here is the struct code…

(
  Struct DirTreeItem (dir, depth, name, treenode) 
  Struct DirTree ( 
  	--empty our tree and set blank path
  	Tree=#(), 
  	pathToParse = "",
  
  	
  	
  	
  	
  	fn parseDirectories root depth = 
  	(
  		--create new array to contain directories
  		local dir_array =#() 
  		--get directories within root
  		dir_array = GetDirectories (root+"/*") 
  		
  		--loop through directories
  		for d in dir_array do 
  		( 
  			--split directory into tokens
  			local splitName = filterstring d "\\" 
  			
  			--get last folder name
  			local dirName = splitName[(splitName.count)] 
  			
  			--add directory to list of tree items at one more depth in than the parent
  			append Tree (DirTreeItem dir:d depth:(depth+1) name:dirName)
  			
  			
  			--try and add it to the child node if we have one, else add it to the parent 
  			try(parseDirectories d (depth+1)) catch(parseDirectories root depth) 
  		)
  	), -- End fn
  
  
  	fn Run =
  	(
  		
  			--st = timestamp()
  			
  			--add the root level entry
  			append Tree (DirTreeItem dir:pathToParse depth:0) 
  			
  			--parse all the directories within our root
  			parseDirectories pathToParse 0   
  			
  			
  			--format "Directories.parseDirectories() completed in [% ms]
" (timestamp()-st) 
  		
  	)
  )

@Pen, not got one handy you fancy sharing for your dotnet tutorial by any chance?

28 Replies
1 Reply
 PEN
(@pen)
Joined: 11 months ago

Posts: 0

Not sure what you are asking about the dotNet tutorial?

Hey Paul, was looking at this tutorial from yourself…

http://paulneale.com/tutorials/dotNet/treeView/dotNetTreeView.htm

and i’m trying to use the above code to make use the dotnet TreeView as a directory lister. Something like this… also from your website… top left

 PEN

Well you don’t need structs, you just need a recursive function to run down the tree of directories and build the treeView nodes as they go.

Have you done recursive functions before?

1 Reply
(@davewortley)
Joined: 11 months ago

Posts: 0

Don’t I need a struct to store the directory info so I can work out what directory was clicked in the tree?

I think I know what you mean by recursive functions, I attempted to write one for the directories but GetDirectories() went through all the sub folders as well.

Have you checked my custom treeview? It does what you want, http://forums.cgsociety.org/showthread.php?f=98&t=678302

wrote this before I refreshed and saw Artur’s response. Regardless this still might help:

try(destroyDialog theRollout)catch()
rollout theRollout "The Rollout" width:300
(
	local TreeNodeClass = dotnetclass "System.Windows.Forms.TreeNode"
	
	dotNetControl tv "system.windows.forms.treeView" height:200
	
	fn GetDirectoryTree dir =
	(
		flt = filterstring dir "\\"
		dirName = flt[flt.count]
		
		t = dotnetobject TreeNodeClass dirName
		t.Tag = dir -- assign the full path to the tag property
		
		subDirs = GetDirectories (dir+@"/*")
		subNodes = for d in subDirs collect GetDirectoryTree d
		t.Nodes.AddRange subNodes
		
		t
	)
	
	on theRollout open do
	(
		n = GetDirectoryTree (getDir #maxroot)
		tv.Nodes.Add n
	)
)
createDialog theRollout
 PEN

Ah I see, well you can use the tab property of each treeView node to store the path for that directory. Then when accessing in that directory you can just refer to the treeView nodes .tab property.

A recursive function is needed to recurse all the directories from a parent down through all the children.

This will print all files and directory paths in Max to the listener.

fn recurseDirs root=
(
	files=getFiles (root+"*.*")
	for f in files do format "	File: %
" (fileNamefromPath f)
	
	dirs=getDirectories (root+"*")
	for d in dirs do 
	(
		format "Dir: %
" d
		recurseDirs d
	)
)
recurseDirs (getDir #maxRoot)
1 Reply
(@jhaywood)
Joined: 11 months ago

Posts: 0

Do you mean “Tag” property?

I think you mean tag with a G

the GetDirectoryTree function in my previous post does exactly this

 PEN

Sorry “tag”

Cheers Gravey, that works just as I needed to.

Thanks for the explanation Paul, didn’t realise you could use tags like that.

Is there a way to expand the first node in the tree? I can use the tv.ExpandAll() to expand everything but I only want the first node expanded.

4 Replies
(@denist)
Joined: 11 months ago

Posts: 0

treenode has Expand and ExpandAll methods.

(@davewortley)
Joined: 11 months ago

Posts: 0

I can only find reference to ExpandAll() and on BeforeExpand and on AfterExpand.

(@denist)
Joined: 11 months ago

Posts: 0

i said “TreeNode”, not “TreeView”. So the syntax is : tv.nodes.item[n].Expand() or tv.nodes.item[n].ExpandAll()

(@davewortley)
Joined: 11 months ago

Posts: 0

Thanks DenisT, where is the best place to find the properties for the treeview and nodes?
For instance I now want to ‘refresh’ my tree system, so I want to clear the tree and replace it with a new one, but can’t find any properties for removing/clearing/deleting nodes.

And one other small thing, can I get the recursive function (Gravey’s) to ignore certain folders, with say name “maps” from being included in the tree.

1 Reply
 PEN
(@pen)
Joined: 11 months ago

Posts: 0

Just need to check the name of the directories just before you recurse them and determine if the name is in an exclude list. You will have to strip the and directory name off the path using something like


dirNames=filterString directoryPath "\\"
dirNames[dirNames.count]
Page 1 / 3