• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Create Treeview Nodes based on string


I have around 20,000 records in a text file. Each line contains something like the following:

   Microwave Ovens\Samsung\FC139STFC\20050809091926296_1.Precaution.pdf
   Microwave Ovens\LG\MS6682NL\Exploded_view.pdf
   Microwave Ovens\LG\Ms324sce Ms325sce\Parts Lists\MS325SCE.csv

   Each of the above is followed by comma delimited date, time, filesize etc

  I need to create Parent/Child nodes in a Treeview using this info. For example,

     Microwave Ovens
   Then populate a Listview with the filename i.e 20050809091926296_1.Precaution.pdf  

   Does anyone have some sample code that demonstrates this, please.


  • 2
  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's fairly straight forward to do this but I don't know how practical it would be as I'm not sure a TreeView can handle that many nodes.  At the very least it would certainly be tedious for a human to have to filter thru that many records...
tvtechAuthor Commented:
If you have some source code, by all means please post it, as I can always mod the code later to import the text file into a MDB. Humans don't need to filter the data, as a search facility will be added later. The main thing is to make it work.


Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's the gist of it...

Option Explicit

Private Sub Form_Load()
    On Error Resume Next
    Dim ff As Integer
    Dim fileName As String
    Dim inputLine As String
    Dim values As Variant
    Dim nodes As Variant
    Dim Node As String
    Dim i As Integer
    Dim n As Node
    Dim parentN As Node
    ff = FreeFile
    fileName = "c:\someFile.txt"
    Open fileName For Input As #ff
    While Not EOF(ff)
        Line Input #ff, inputLine
        values = Split(inputLine, ",")
        nodes = Split(values(0), "\")
        Set parentN = Nothing
        For i = 0 To UBound(nodes)
            If i < UBound(nodes) Then
                Set n = Nothing
                Set n = TreeView1.nodes(nodes(i))
                If n Is Nothing Then
                    If parentN Is Nothing Then
                        Set n = TreeView1.nodes.Add(, , nodes(i), nodes(i))
                        Set n = TreeView1.nodes.Add(parentN, tvwChild, nodes(i), nodes(i))
                    End If
                End If
                Set parentN = n
                parentN.Tag = nodes(i)
            End If
        Next i
    Close #ff
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If Node.Tag <> "" Then
        Debug.Print Node.Tag
    End If
End Sub
tvtechAuthor Commented:
Idle_Mind, nice work. Many thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now