Add SubItems to TreeView
I'm sure this sounds like a n00b question, but how can I add helper items programmatically when populating a TreeView list in VB.NET 3.5? I have the following code, but I was unable to figure out how to add sub-items for each of the folders / files that I populate the TreeView with:
Private Sub AddToList(ByVal targetDirectory As String, ByVal boolFiles As Boolean, Optional ByVal recur As Boolean = False)
Dim shortName As String
TreeView1.Items.Add(targetDirectory)
//Add subitems under here
If Directory.GetDirectories(targetDirectory).Length > 0 Then
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
shortName = subdirectory.Remove(0, subdirectory.LastIndexOf("\") + 1)
TreeView1.Items.Add(shortName)
AddToList(subdirectory, False, True)
If boolFiles = True Then AddToList(subdirectory, boolFiles)
Next
End If
End Sub
To clarify, I want my TreeView to look the same as Windows Explorer. I appreciate any help!
Thanks in advance! JFV
+1
source to share
2 answers
You need to use TreeNode objects and add children to the parent TreeNode, not add everything to the TreeView directly. Check out this example .
+2
source to share