2010-07-16 63 views
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告訴我的方式。當我運行這段代碼時,當它開始嘗試將文件添加到樹視圖時,出現錯誤「未將對象引用設置爲對象的實例」。

回答

2

只是快速瀏覽一下,你似乎將你找到的每個節點添加到樹的根級。

TreeView1.Nodes.Add(nodeOjb) 

你應該做的是通過遞歸調用你正在使用的當前節點並使用它來添加找到的下一級節點。

currentNode.Add(nodeObj) 

這種方式可以讓孩子添加到節點。而不是總是將所有節點添加到樹的根部。

編輯:這裏是你需要做的PopulateTree方法的變化:

Private Sub PopulateTree(currentNode As TreeNode, path__1 As String, subfoldercount As Integer) 
    Dim items As String() = Nothing 
    items = Directory.GetFileSystemEntries(path__1) 

    Dim nodeParent As TreeNodeCollection = If((currentNode IsNot Nothing), currentNode.Nodes, Me.TreeView1.Nodes) 

    For Each itm As String In items 
     If Directory.Exists(itm) Then 
      Dim nodeOjb = New TreeNode() 
      nodeOjb.Text = "FOLDER :: " & subfoldercount & " :: " & itm 
      nodeOjb.ForeColor = Color.Blue 
      nodeParent.Add(nodeOjb) 
      Me.PopulateTree(nodeOjb, itm, subfoldercount + 1) 
     Else 
      Dim nodeOjb = New TreeNode() 
      nodeOjb.Text = "FILE :: " & subfoldercount & " :: " & itm 
      Select Case Path.GetExtension(itm) 
       Case ".txt" 
        nodeOjb.ForeColor = Color.Orange 
        nodeParent.Add(nodeOjb) 
        Exit Select 
       Case ".png" 
        nodeOjb.ForeColor = Color.Red 
        nodeParent.Add(nodeOjb) 
        Exit Select 
       Case ".ico" 
        nodeOjb.ForeColor = Color.Green 
        nodeParent.Add(nodeOjb) 
        Exit Select 
       Case ".url" 
        nodeOjb.ForeColor = Color.Black 
        nodeParent.Add(nodeOjb) 
        Exit Select 
      End Select 
     End If 
    Next 
End Sub 

然後當你調用它的第一次,你這樣做是這樣的:

PopulateTree(Nothing, "", 0) 

你傳遞第一次調用的空引用,所以它使用父級。第二個參數是你的路徑。

p.s.我主要是一個C#人,所以我使用轉換器來更改代碼。它應該可以工作,但你可能需要稍微調整一下。

+0

非常感謝。如果我是註冊用戶,我會爲您投票。反正+1 – muckdog12 2010-07-16 23:08:11

相關問題