Link to home
Start Free TrialLog in
Avatar of tkeeler1
tkeeler1

asked on

TreeView Hiding Top Node

Hi,

  How can I hide the top node of a TreeView so it doesn't show up?  Currently I have the following:

Private Function FillTree(MyFolder)
Dim fso As FileSystemObject
Dim target_folder As Folder
Dim target_node As Node
Dim txt As String

    Screen.MousePointer = vbHourglass
    trvResults.Visible = False
    DoEvents

    trvResults.Nodes.Clear

    Set fso = New FileSystemObject
    Set target_folder = fso.GetFolder(MyFolder)

    txt = target_folder.ParentFolder & "\" & target_folder.Name
    Set target_node = trvResults.Nodes.Add(, , , txt)
    target_node.Expanded = True

    ListFileInfo trvResults, target_node, target_folder

    trvResults.Visible = True
    trvResults.Enabled = False
    Screen.MousePointer = vbDefault
   
End Function

Private Sub ListFileInfo(ByVal trv As TreeView, ByVal parent_node As Node, ByVal parent_folder As Folder)
Dim txt As String
Dim child_folder As Folder
Dim child_file As File
Dim child_node As Node

    ' Search subdirectories.
    For Each child_folder In parent_folder.SubFolders
        txt = child_folder.Name
        Set child_node = trv.Nodes.Add( _
            parent_node, tvwChild, , txt)
        ListFileInfo trv, child_node, child_folder
    Next child_folder

    ' List the files.
    For Each child_file In parent_folder.Files
        txt = child_file.Name
        trv.Nodes.Add parent_node, tvwChild, , txt
    Next child_file
    'Node1.Expanded = True
End Sub

------------------
As you can see, the file structure is like this:
MYFolderPath
--subfolder1
--subfolder2
--file1
--file2

I would like to delete MYFolderPath and just show everything underneath.  Any ideas?

Thank you,
TK


ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tkeeler1
tkeeler1

ASKER

Hi,

  I couldn't get this to work.  While "txt" shows the correct ParentFolder path, it seems to get lost on the subsequent lines of code, and "trvResults.Visible" never becomes true...?  Please see my code below and let me know what might be wrong.

Thanks for your help!
TK
------------------------
Private Function FillTree(MyFolder)
Dim fso As FileSystemObject
Dim target_folder As Folder
Dim target_node As Node
Dim txt As String

    Screen.MousePointer = vbHourglass
    trvResults.Visible = False
    DoEvents

    trvResults.Nodes.Clear

    Set fso = New FileSystemObject
    Set target_folder = fso.GetFolder(MyFolder)

    txt = target_folder.ParentFolder & "\" & target_folder.Name
    'MsgBox ("txt = " & txt)  '--shows up with correct path
    Set target_node = trvResults.Nodes.Add(, , , txt)
    target_node.Expanded = True

    ListFileInfo trvResults, Nothing, target_folder

    trvResults.Visible = True
    trvResults.Enabled = False
    Screen.MousePointer = vbDefault
   
   
End Function

Private Sub ListFileInfo(ByVal trv As TreeView, ByVal parent_node As Node, ByVal parent_folder As Folder)
Dim txt As String
Dim child_folder As Folder
Dim child_file As File
Dim child_node As Node

    For Each child_folder In parent_folder.SubFolders
        txt = child_folder.Name
        If parent_node Is Nothing Then
        Set child_node = trv.Nodes.Add( _
                    , , , txt)
        Else
        Set child_node = trv.Nodes.Add( _
                    parent_node, tvwChild, , txt)
        End If
        ListFileInfo trv, child_node, child_folder
    Next child_folder

    For Each child_file In parent_folder.Files
        txt = child_file.Name
        trv.Nodes.Add parent_node, tvwChild, , txt
    Next child_file
    'Node1.Expanded = True
End Sub
Hi again!

Nevermind, I figured out I just needed to add the "If" statement:

       If parent_node Is Nothing Then
        Set child_node = trv.Nodes.Add( _
                    , , , txt)
        Else
        Set child_node = trv.Nodes.Add( _
                    parent_node, tvwChild, , txt)
        End If

to the child_file also (in addition to the child_node).

It works.... Thanks for your help!
TK