We help IT Professionals succeed at work.

Create Treeview Nodes based on string

tvtech
tvtech asked
on
Medium Priority
255 Views
Last Modified: 2008-01-09
Hi,

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
       \Samsung
       \FC139STFC
           
   Then populate a Listview with the filename i.e 20050809091926296_1.Precaution.pdf  

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

Thanks.

TJ
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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...

Author

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.

Thanks.

TJ
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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))
                    Else
                        Set n = TreeView1.nodes.Add(parentN, tvwChild, nodes(i), nodes(i))
                    End If
                End If
                Set parentN = n
            Else
                parentN.Tag = nodes(i)
            End If
        Next i
    Wend
    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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Idle_Mind, nice work. Many thanks.

TJ
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.