Came up with some simple to use treenode subs that simply require 2 simple things:
1. a path, usually as a computer path, but you could easily trick it out and datatable paths (it would require a few very simple changes everywhere you see Directory.GetDirectories)
2. the second sub simply needs to be placed in the treeview's BeforeExpand event
Everything is broke down so it is very easy to follow and see what's going on, i'll add comments after lunch if i get a chance, i'm basically killing time for coffee to finish making for now.
Simply add the first sub to your form load event with:
First Sub:
Populate TreeView Code:
Public Sub populateTreeView(ByVal _treeView As TreeView, ByVal _path As String)
Try
For Each dirParent As String In Directory.GetDirectories(_path)
Dim parentNode As New TreeNode(dirParent.Substring(_path.Length + 1))
trvMain.Nodes.Add(parentNode)
Try
'Works for Music/Pics/Video Folders in Windows 7/Vista
If dirParent.Contains("Documents") Then
If dirParent.Contains("Music") Then
dirParent = dirParent.Replace("Documents\My ", "")
End If
If dirParent.Contains("Pictures") Then
dirParent = dirParent.Replace("Documents\My ", "")
End If
If dirParent.Contains("Videos") Then
dirParent = dirParent.Replace("Documents\My ", "")
End If
End If
For Each dirChild As String In Directory.GetDirectories(dirParent)
Dim path As String
Try
path = dirParent
Dim childNode As New TreeNode(dirChild.Substring(dirParent.Length + 1))
parentNode.Nodes.Add(childNode)
Catch ex As Exception
Dim exMessage As String = ex.Message & Environment.NewLine
'RelayException
End Try
Next
Catch ex As Exception
Dim exMessage As String = ex.Message & Environment.NewLine
'RelayException
End Try
Next
Catch ex As Exception
Dim exMessage As String = ex.Message & Environment.NewLine
'RelayException
End Try
End Sub
Simply add the second sub to your treeview BeforeExpand event with:
Treeview BeforeExpand Event Code:
populateSubTreeView(e.node, yourPath)
Second Sub:
Populate Sub Tree View Code:
Public Sub populateSubTreeView(ByVal eNode As TreeNode, ByVal _path As String)
Try
Dim newPath As String = _path
If Not eNode.FullPath Is Nothing Then
newPath = String.Concat(newPath, "\", eNode.FullPath)
Try
For Each child In eNode.Nodes
Dim subNode As String = child.FullPath.Substring(eNode.FullPath.Length + 1)
Dim subPath As String = String.Concat(newPath, "\", subNode)
Try
For Each folder In Directory.GetDirectories(subPath)
Dim childNode As New TreeNode(folder.Substring(subPath.Length + 1))
child.Nodes.Add(childNode)
Next
Catch ex As Exception
Dim exMessage As String = ex.Message
'RelayException
End Try
Next
Catch ex As Exception
Dim exMessage As String = ex.Message
'RelayException
End Try
End If
Catch ex As Exception
Dim exMessage As String = ex.Message
'RelayException
End Try
End Sub
HINT: use ctrl+f to replace all the 'RelayException marks with your own way (exp. Console.WriteLine(exMessage))