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