Solved

TreeView Hiding Top Node

Posted on 2006-06-15
3
1,164 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

832 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