Solved

TreeView Hiding Top Node

Posted on 2006-06-15
3
1,155 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
ID: 16917749
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
ID: 16922378
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
ID: 16922650
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

16 Experts available now in Live!

Get 1:1 Help Now