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.
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: