Treenode从崩溃扩展时复制

时间:2010-07-01 19:19:37

标签: vb.net treenode

从折叠状态展开时,子节点中的项似乎每次都会复制。我认为问题是在扩展之前需要清除内存。谁能明白为什么会发生这种事件。在此先感谢。

  Public Sub FillTree(ByVal s As String)
        Dim nodeText As String = ""
        Dim sb As New C_StringBuilder
        With My.Computer.FileSystem
            For i As Integer = 0 To .Drives.Count - 1
                '** Build the drive's node text
                sb.ClearText()
                sb.AppendText(.Drives(i).Name.ToString)
                nodeText = sb.FullText
                'Check to see if DropDown Selection is the same as what has been read into i
                If (sb.FullText = s) Then
                    '** Add the drive to the treeview
                    Dim driveNode As TreeNode
                    tvFolders.Nodes.Clear()
                    driveNode = tvFolders.Nodes.Add(nodeText)
                    driveNode.Tag = .Drives(i).Name
                    '** Add the next level of subfolders
                    ListLocalSubFolders(driveNode, .Drives(i).Name)
                End If
            Next
        End With
    End Sub

Private Sub ListLocalSubFolders(ByVal ParentNode As TreeNode, ByVal sParentPath As String)
    '    ' Add all local subfolders below the passed Local treeview node
    Dim s As String
    Try
        For Each s In Directory.GetDirectories(sParentPath)
            Dim childNode As TreeNode
            childNode = ParentNode.Nodes.Add(FilenameFromPath(s))
            childNode = Nothing
        Next
    Catch ex As Exception
    End Try
End Sub

Private Sub tvFolders_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvFolders.BeforeExpand

    ' Populate all child nodes below the selected node
    Dim parentPath As String = e.Node.Tag
    Dim childNode As TreeNode = e.Node.FirstNode
    Do While childNode IsNot Nothing
        ListLocalSubFolders(childNode, parentPath & childNode.Text)
        childNode = childNode.NextNode
    Loop


End Sub

1 个答案:

答案 0 :(得分:0)

必须这一行:

ListLocalSubFolders(childNode, parentPath & childNode.Text)

看起来不像这样:

ListLocalSubFolders(childNode, childNode.Text)

否则你会得到f.e.像这样:“C:\ C:\ $ Recycle.Bin”