我确定这听起来像是一个n00b问题,但是如何在VB.NET 3.5中填充TreeView列表时以编程方式添加子项呢?我有以下代码,但无法弄清楚如何为我填充TreeView的每个文件夹/文件添加子项:
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
为了澄清,我希望我的TreeView看起来与Windows资源管理器外观类似。我感谢任何帮助!
提前致谢! JFV
答案 0 :(得分:2)
您需要使用TreeNode对象,并将子项添加到父TreeNode,而不是直接将所有内容添加到TreeView。查看this example。
答案 1 :(得分:1)
这是哪个TreeView?在winforms中,您只需从Add中捕获返回的TreeNode,并将更多项添加到Nodes属性中:
TreeNode parent = treeView.Nodes.Add("parent");
parent.Nodes.Add("child");