Question How can I add child nodes to a treeview from a directory path?

tud0ne

New member
Joined
Jun 7, 2009
Messages
2
Programming Experience
Beginner
Hi

I have a treeview, a button and a textbox. When you click button1 a folderbrowserdialog opens and after choosing the folder the path to the folder and anything in it is show in the textbox and with this code:
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sDirs() As String
        Dim Dir
        Dim fdlg As FolderBrowserDialog = New FolderBrowserDialog()
        If fdlg.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = fdlg.SelectedPath
        End If
        Dim i As Integer
        Dir = TextBox1.Text
        sDirs = System.IO.Directory.GetDirectories(Dir)
        For i = 0 To UBound(sDirs)
            TreeView1.Nodes.Add(sDirs(i))
        Next
    End Sub
adapted from http://www.vbdotnetforums.com/listviews-treeviews/34336-major-treeview-list-view-head-ache.html I managed to make the treeview display the folder and the sub folders... but it isn't very hierarchial. Check attached pictures. Image1 shows how it is and image2 shows how I want it to be. Can anyone tell me how to get it to look like image 2?...Image2 was made in Windows Explorer. Image1 is a screenshot of the runing program that I am trying to make. I have been googling around but no luck so far. Practically I think what I shoul be asking is how to create child nodes of subfolders. Correct me if these aren't the proper terms I am relatively new to programming. The program is going to copy whatever the user checks in the treeview into another folder chosen by the user. I am using Visual Basic 2008 Express.
Thanks in advance:D.
 

Attachments

  • Image2.bmp
    84.1 KB · Views: 39
  • Image1.GIF
    Image1.GIF
    4.5 KB · Views: 34
Thanks for the reply newguy. Now I stumbled upon a new problem. The user will check a file or folder in the treeview. When he clicks a button only those checked items will be copied and pasted to a destination also chosen by the user in another treeview by checking a folder. Now if the treeviews are refreshed with Moreau's code then the checked items are no longer checked. And also if you collapse and then expand again a node, all checked items in that node are then unchecked. Any suggestions?
This is the code used to refresh the treeview:
VB.NET:
Private Sub AddDrives(ByVal myTreeview As TreeView)
        With myTreeview
            .Nodes.Clear()
            For Each drives As String In Directory.GetLogicalDrives
                With .Nodes.Add(drives)
                    .Nodes.Add("DUMMY ENTRY")
                End With
            Next
        End With
    End Sub
And this is the code used to add items to nodes when they are expanded:
VB.NET:
Private Sub Treeview_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand, TreeView2.BeforeExpand
        If sender Is TreeView1 Then
            Try
                e.Node.Nodes.Clear()
                ' Go out and get the folder and file names.
                If AddFolders(e.Node) Then
                    AddFiles(e.Node)
                End If
            Catch ex As IOException
                ' Perhaps the drive isn't ready. In that case, simply disregard the error.
            Catch ex As Exception
                ' If any other error occurs, display an alert.
                MsgBox(ex.ToString, , Me.Text)
            End Try
        Else
            Try
                e.Node.Nodes.Clear()
                ' Go out and get the folder and file names.
                AddFolders(e.Node)
            Catch ex As IOException
                ' Perhaps the drive isn't ready. In that case, simply disregard the error.
            Catch ex As Exception
                ' If any other error occurs, display an alert.
                MsgBox(ex.ToString, , Me.Text)
            End Try
        End If
    End Sub
Thanks in advance:D.
 
Back
Top