0
我希望能够创建一个树形视图,使其节点可以形成计算机上的目录。在下面的代码中,我能够将所有文件都放入列表中,但我无法获得正确的文件夹。我的意思是在你的用户目录中,你有副导演,如文件,音乐和图片。当你运行这个代码时,它将它们分别显示为它们自己的节点,而不是嵌套的。我希望这是有道理的。感谢在VB.NET请。创建一个类似Visual Studio的解决方案资源管理器
Private Sub PopulateTree(ByVal path As String, ByVal subfoldercount As Integer)
Dim items() As String
items = Directory.GetFileSystemEntries(path)
Dim itm As String
TreeVeiw1.Nodes.Add(path)
Dim currentnode As TreeNode = TreeView1.Nodes.Item(0)
For Each itm In items
If Directory.Exists(itm) Then
Dim nodeOjb As New TreeNode
nodeOjb.Text = "FOLDER :: " & subfoldercount & " :: " & itm
nodeOjb.ForeColor = Color.Blue
currentnode.Nodes.Add(nodeOjb)
PopulateTree(itm, subfoldercount + 1)
Else
Dim nodeOjb As New TreeNode
nodeOjb.Text = "FILE :: " & subfoldercount & " :: " & itm
Select Case My.Computer.FileSystem.GetFileInfo(itm).Extension
Case ".txt"
nodeOjb.ForeColor = Color.Orange
currentnode.Add(nodeOjb)
Case ".png"
nodeOjb.ForeColor = Color.Red
currentnode.Add(nodeOjb)
Case ".ico"
nodeOjb.ForeColor = Color.Green
currentnode.Add(nodeOjb)
Case ".url"
nodeOjb.ForeColor = Color.Black
currentnode.Add(nodeOjb)
End Select
End If
Next
End Sub
将代码更改为spinion告诉我的方式。当我运行这段代码时,当它开始尝试将文件添加到树视图时,出现错误“未将对象引用设置为对象的实例”。
非常感谢。如果我是注册用户,我会为您投票。反正+1 – muckdog12 2010-07-16 23:08:11