views:

31

answers:

1

I built a treenode to be populated from my network drives, from this treeview I would like to populate another treeview to show the files when the first one is selected. For example, if the user were to click on the c:\TestFolder then the second treeview would show the TestFolder, all sub folders and files. Below is my code, thanks.

Imports System Imports System.IO Public Class F_Treeview_Demo Public IsSomethingChecked As Boolean = False Private fbIgnoreClick As Boolean = False Private fbLoadingForm As Boolean = True Private sRead As String Public n As TreeNode Public l As New List(Of String) Public drivesTree As New List(Of String)

Private Sub F_Treeview_Demo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Initialize the local directory treeview

    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 combo box with drive's node text
            sb.ClearText()
            sb.AppendText(.Drives(i).DriveType.ToString)
            sb.AppendText(RemoveTrailingChar(.Drives(i).Name, gtBACKSLASH))
            cmbDrives.Items.Add(sb.FullText)


        Next
    End With

End Sub
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).DriveType.ToString)
            sb.AppendText(RemoveTrailingChar(.Drives(i).Name, gtBACKSLASH))

            nodeText = sb.FullText
            'nodeText = Me.tvFolders.SelectedNode.Text
            '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.ImageIndex = 0
                driveNode.SelectedImageIndex = 1
                driveNode.Tag = .Drives(i).Name


                Dim FolderNode As String = ""
                'Dim FirstNode As TreeNode
                'tvFolders.Nodes.Clear()
                'FirstNode = tvFolders.Nodes.Add(nodeText)

                '** Add the next level of subfolders
                ListLocalSubFolders(driveNode, .Drives(i).Name)
                driveNode = Nothing
            End If
        Next
    End With
End Sub

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

    ' Display the path for the selected node
    ' lblLocalPath.Text = e.Node.Tag
    ' Populate all child nodes below the selected node
    Dim parentPath As String = AddChar(e.Node.Tag)
    tvFolders.BeginUpdate()
    Dim childNode As TreeNode = e.Node.FirstNode
    'this i added
    Dim smallNode As TreeNode = e.Node.FirstNode

    Do While childNode IsNot Nothing
        ListLocalSubFolders(childNode, parentPath & childNode.Text)
        childNode = childNode.NextNode
        ''this i added
        ListLocalFiles(smallNode, parentPath & smallNode.Text)

    Loop
    tvFolders.EndUpdate()
    tvFolders.Refresh()
    ' Select the node being expanded
    tvFolders.SelectedNode = e.Node
End Sub

Private Sub ListLocalFiles(ByVal ParentNode As TreeNode, ByVal PParentPath As String)

    Dim FileNode As String = ""
    Try
        For Each FileNode In Directory.GetFiles(PParentPath)
            Dim smallNode As TreeNode
            smallNode = ParentNode.Nodes.Add(FilenameFromPath(FileNode))
            With smallNode
                .ImageIndex = 0
                .SelectedImageIndex = 1
                .Tag = FileNode
            End With
            smallNode = Nothing
        Next
    Catch ex As Exception
    End Try

End Sub

Private Sub ListLocalSubFolders(ByVal ParentNode As TreeNode, ByVal ParentPath As String)
    ' Add all local subfolders below the passed Local treeview node
    Dim FolderNode As String = ""
    Try
        For Each FolderNode In Directory.GetDirectories(ParentPath)
            Dim childNode As TreeNode
            childNode = ParentNode.Nodes.Add(FilenameFromPath(FolderNode))
            With childNode
                .ImageIndex = 0
                .SelectedImageIndex = 1
                .Tag = FolderNode
            End With
            childNode = Nothing
        Next
    Catch ex As Exception
    End Try

End Sub



Private Sub lblLocalPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub grpLocalFileSystem_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpLocalFileSystem.Enter

End Sub


Private Sub cmbDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDrives.SelectedIndexChanged
    'populate tree view from user selection
    FillTree(Me.cmbDrives.SelectedItem.ToString)
End Sub

Private Sub checkBox_isSelected()

End Sub

Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click

    ListBox1.Items.Clear()

    CallRecursive(tvFolders)

    If (IsSomethingChecked = False) Then
        MessageBox.Show("Please select an item to replicate.")
    End If

End Sub

'End Function
Private Sub PrintRecursive(ByVal n As TreeNode)
    System.Diagnostics.Debug.WriteLine(n.Text)

    If (n.Checked = True) Then
        IsSomethingChecked = True
        sRead = n.FullPath
        l.Add(sRead)
        'If (n.Checked = False) Then
        ' ListBox1.Items.Add(sRead)
        ' MessageBox.Show(n.FullPath)
        'End If

        ListBox1.Items.Add(sRead)
        '  MessageBox.Show(sRead)

        ' Next

    End If
    Dim aNode As TreeNode
    For Each aNode In n.Nodes
        PrintRecursive(aNode)


    Next

End Sub

' Call the procedure using the top nodes of the treeview. 
Private Sub CallRecursive(ByVal aTreeView As TreeView)
    Dim n As TreeNode
    For Each n In aTreeView.Nodes
        PrintRecursive(n)
        If IsSomethingChecked = True Then
            Exit Sub
        End If
    Next
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ListBox1.Items.Clear()

End Sub

Private Sub tvFiles_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvFiles.BeforeSelect
    FillTree(Me.tvFolders.SelectedNode.Tag)
End Sub


Private Sub tvFolders_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvFolders.NodeMouseClick

End Sub

Private Sub tvFiles_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFiles.AfterSelect

End Sub

End Class

A: 

As I understand it you've created a treeview with nodes in it and when a node is selected that node with all it's children should be shown in another TreeView?

If so, just add the NodeMouseClick event handler and then add the code:

treeView2.Nodes.Clear()
treeView2.Nodes.Add(Ctype(e.Node.Clone(), TreeNode))

If the first TreeView don't already contain the actual files in the directories you'd also have to add code after this to loop through all the nodes in treeView2 and call something like Directory.GetFiles and add childnodes with the filenames.

For future reference, it's good to add code to the question, but I'd suggest trying to cut it down to only the really relevant bits if possible.

ho1
Thank you very much! This is actually what I was looking for.
jpavlov
Are you familuar with this error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.After I clone the tree and highlight a node I recieve this.
jpavlov
@jpavlov: You mean when you highlight a node in treeview number 2? Might be that some data don't get cloned correctly (maybe the images or data you put in the tags of the nodes or something). Try to find out exactly which line the error occurs (I assume it might be in your `BeforeExpand` handler or something similar, so put in some breakpoints and step through).
ho1
Thank you once again. My mistake was pure ignorance, I copied the code twice, once in a before_select and once in the after_select.Thank you very much.
jpavlov