?
Solved

TreeView Hiding Top Node

Posted on 2006-06-15
3
Medium Priority
?
1,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

650 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