The child nodes are there when first populated but when drives are changed or re loaded a second time, that is when they are missing, so how do I reset the nodes to show child nodes every time

This is my code, can anybody see a problem, any help much appreciated

thistreeview.nodes.clear
Dim basenode As System.Windows.Forms.TreeNode
        If IO.Directory.Exists(path) Then
            If path.Length <= 3 Then
                basenode = ThisTreeview.Nodes.Add(path)
            Else
                basenode = ThisTreeview.Nodes.Add(My.Computer.FileSystem.GetName(path))
            End If
            basenode.Tag = path
            Treeview_LoadDir(path, basenode)
        Else
            Throw New System.IO.DirectoryNotFoundException()
        End If

        ThisTreeview.Sort()
        ThisTreeview.TopNode.Expand()



    Public Sub Treeview_LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)

        On Error Resume Next
        Dim Dir As String
        Dim Index As Integer
        If Node.Nodes.Count = 0 Then
            For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
                Index = Dir.LastIndexOf("\")
                Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
                Node.LastNode.Tag = Dir
                Node.LastNode.ImageIndex = 0
            Next
        End If

Firstly, please use [code]

[/code] tags around your code.

You could use the FileSystemWatcher to monitor for changes.
But it might be easier to just refresh the treeview when the form is Activated.
Also, I do not think that your current code goes down further that one level.
You might need to recursively call Treeview_LoadDir to scan for lower child nodes.

My programming expertise is only moderate but open to suggestions for improvement, I did not understand your comment:
Firstly, please use

tags around your code.
please explain.

Also the form is only loaded once and the tree is populated by changing drives from a list box of drives, the nodes are shown correctly when form first loads, but does not show + against relative folders second time it is populated for the same drive.

Firstly, please use

tags around your code.

In the forums here at Daniweb it is required that you put code tags around your code so that it is formatted as code.
(On your post you'll see that the administrator has kindly added the tags for you.)

It is not clear from the code you have posted where the problem might be.
Please post your code that handles the change in the drive selection on the list box.
Also, please post your original code again, but this time post the whole subroutine(s).

Use the "Expand" method as in trvMyTree.Nodes(0).Expand

First I have an apology, I failed to include some code, so my apologies for that.

Here is the full code

step 1
call desired drive

Treeview_LoadFolderTree(me.TreeView1, Mid(lstDrives.Text, 1, 3))

, ie "c:\"

step 2

in module

Public Sub Treeview_LoadFolderTree(ByVal ThisTreeview As System.Windows.Forms.TreeView, ByVal path As String)

       
        Treeview_Parent_Folder = path

       
        ThisTreeview.Nodes.Clear()
       
        Dim basenode As New System.Windows.Forms.TreeNode
        If IO.Directory.Exists(path) Then
            If path.Length <= 3 Then
                basenode = ThisTreeview.Nodes.Add(path)
            Else
                basenode = ThisTreeview.Nodes.Add(My.Computer.FileSystem.GetName(path))
            End If
            basenode.Tag = path
            Treeview_LoadDir(path, basenode)
        Else
            Throw New System.IO.DirectoryNotFoundException()
        End If

       
        ThisTreeview.Sort()
        ThisTreeview.TopNode.Expand()

    End Sub

step 3
in module

Public Sub Treeview_LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)

        On Error Resume Next
        Dim Dir As String
        Dim Index As Integer
       
        If Node.Nodes.Count = 0 Then
            For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
                Index = Dir.LastIndexOf("\")
                Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
                Node.LastNode.Tag = Dir
                Node.LastNode.ImageIndex = 0
                Console.WriteLine("Treeview_LoadDir" & " " & Dir)

            Next
        End If

    End Sub

step 4

from Main form on Treeview1After expand

Private Sub TreeView1_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterExpand

        Treeview_Expand(e)

     
    End Sub

step 5

In Module

Sub Treeview_Expand(ByVal e As System.Windows.Forms.TreeViewEventArgs)

        Dim n As System.Windows.Forms.TreeNode
        For Each n In e.Node.Nodes
            Treeview_LoadDir(n.Tag, n)
            Console.WriteLine("Treeview_Expand" & " " & n.Tag)
        Next

    End Sub

It now appears that the problem is in step 5 where n.tag is null second time around, so somewhere something needs to be reset so the n.tag is valid

console shows "Treeview_Expand & Directory" 1st time, but only shows "Treeview_Expand" second time around, maybe there is a problem with e.node.nodes?

I have now abandoned the above code & found a far better solution, there are a few gremlins to resolve. When I have the entire solution solved, will post a summary of what I have found.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.