Solved

TreeView Hiding Top Node

Posted on 2006-06-15
3
1,144 Views
Last Modified: 2008-09-16
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


0
Comment
Question by:tkeeler1
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
Comment Utility
well, don't use a root node then.

in short:


change this line:
Set child_node = trv.Nodes.Add( _
            parent_node, tvwChild, , txt)

into this:

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



and call the procedure without target_node

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

    ListFileInfo trvResults, nothing, target_folder


0
 

Author Comment

by:tkeeler1
Comment Utility
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
0
 

Author Comment

by:tkeeler1
Comment Utility
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now