Need a hand with TreeView Control

jango_fett

Member
Joined
Jun 15, 2005
Messages
23
Programming Experience
Beginner
Need a hand with TreeView Control - [RESOLVED]

Hi, all. This is a little embarrassing but since it’s the first time I ever use the TreeView control I guess it doesn’t hurt to seek out some help. What if I had a TreeView control to show all directories sub-directories within a provided path? As a recent experience has taught me, I believe recursion is the best choice. I tried creating a recursive function, but so far no luck. Here’s the code. I used the C:\ root as example. Thanks.

HTML:
Private Function ReturnTreeNode(ByVal MyNode As TreeNode, ByVal DirTree As String) As TreeNode
	 Dim NameDir As String, N As Integer
	 If IO.Directory.Exists(DirArbol) Then
		  N = DirTree.LastIndexOf(IO.Path.DirectorySeparatorChar) + 1
		  NameDir = DirArbol.Substring(N)
		  MyNode.Nodes.Add(NameDir)
		  Dim SubDirs() As String = IO.Directory.GetDirectories(DirTree)
		  For Each SubDir As String In SubDirs
			 N = SubDir.LastIndexOf(IO.Path.DirectorySeparatorChar) + 1
			   NameDir =  SubDir.Substring(N)
			 MyNode.Nodes.Insert(MyNode.LastNode.Index, Me.ReturnTreeNode(MyNode.LastNode, SubDir))
		  Next
	 End If
	 Return MyNode
End Function

Private Sub FormView_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
	 Dim FirstNode As New TreeNode, MyTreeView As New TreeView
	 FirstNode = ReturnTreeNode(FirstNode, "C:\")
	 MyTreeView.Nodes.Add(FirstNode)
End Sub
 
Last edited:
First of all, it doesn't really make sense to pass in a node ByVal and then return that node. Either pass in the node ByRef and edit it or don't pass a node at all and create a new node in the function and return that. Here's an example that accepts only the root folder:
VB.NET:
	[color=Blue]Private Function[/color] GetFolderTree([color=Blue]ByVal[/color] root [color=Blue]As String[/color]) [color=Blue]As[/color] TreeNode
		[color=Blue]Dim[/color] rootNode [color=Blue]As New[/color] TreeNode

		[color=Blue]If[/color] root.EndsWith(":"c) [color=Blue]Then[/color]
			[color=Green]'root is a drive.[/color]
			rootNode.Text = root
		[color=Blue]Else[/color]
			[color=Green]'root is a folder.[/color]
		    rootNode.Text = root.Substring(root.IndexOf(IO.Path.DirectorySeparatorChar) + 1)
		[color=Blue]End If[/color]

		[color=Green]'Add a child node for each subfolder.[/color]
		[color=Blue]For Each[/color] subfolder [color=Blue]As String In[/color] IO.Directory.GetDirectories(root)
			rootNode.Nodes.Add([color=Blue]Me[/color].GetFolderTree(subfolder))
		[color=Blue]Next[/color]

		[color=Blue]Return[/color] rootNode
	[color=Blue]End Function[/color]
Call this using something like:
VB.NET:
[color=Blue]Dim[/color] tree [color=Blue]As[/color] TreeNode = [color=Blue]Me[/color].GetFolderTree("C:")
 
Last edited:
Thanks again

Hi, jmcilhinney. Thanks for replying. Good point about the ByVal parameter. I guess your approach is more adequate. However, I still need to get all subfolders of each folder within the path and it doesn't need to be the root folder. Using your example, I wrote the following code. It is still incomplete. Thanks for all your help.

HTML:
Private Sub DisplayTree(ByRef MyNode As TreeNode, ByVal DirTree As String)
   Dim NameDir As IO.DirectoryInfo
   If IO.Directory.Exists(DirTree) Then
	  For Each MyDir As String In IO.Directory.GetDirectories(DirTree)
		 NameDir = New IO.DirectoryInfo(MyDir)
		 MyNode.Nodes.Add(NameDir.Name)
	  Next
   End If
End Sub
 
Half the problem solved

Hi, again. I've just come up with the following code. It is recursive and brings all folders and subfolders within a specified path. Nevertheless, it has one tiny problem. It duplicates the tree from the specified path. I can't really say why.

DK

HTML:
Private Sub DisplayTree(ByRef MyNode As TreeNode, ByVal DirTree As String)
  Dim NameDir As IO.DirectoryInfo
  If IO.Directory.Exists(DirTree) Then
	For Each MyDir As String In IO.Directory.GetDirectories(DirTree)
	  NameDir = New IO.DirectoryInfo(MyDir)
	  Me.DisplayTree(MyNode.Nodes.Add(NameDir.Name), MyDir)
	Next
  End If
End Sub
 
My code will do exactly what you want. Have you tried using it? You can pass it any folder, drive root or not, and it will give you the entire tree with that folder as the root. Look at it step by step. First it creates the root node. Then, for each subfolder, it calls itself to create a child node. Now, each of those calls will also call the function for each subfolder of that subfolder, and so on and so on.

I think most experienced developers would tell you that it is better to use a function than a procedure in this case. There is no point creating an object specifically to pass ByRef if it has no use before the call. The only time you would do that is if you needed the return value for something else, like a boolean that indicates that the call succeeded or failed.
 
Thanks again, dude

Thanks again, jmcilhinney. I tried using your code again as you suggested. I don’t what happened, but somehow it didn’t work the first time. But now it does, superbly I’d have to say. I guess the mistake was mine. Thanks for all your help and for making programming much more fun.

DK
 
Back
Top