Announcement

Collapse
No announcement yet.

Need code to load Treeview from file path list

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need code to load Treeview from file path list

    Hi Everyone,

    I'm trying not to be lazy but I'll ask, does anyone have some code that they're willing to share that will take an array of file paths and load them into a Treeview so that the Treeview layout is equal to the directory structure.

    I have a string array of file paths (sorted and capitalized) and I need to walk the array from the first element to the last and create the correct nodes in the correct relationship (parent/child).

    .... just trying to save myself a few hours of headache trying to figure out this algorithm.

    Thanks!
    Paul Squires
    FireFly Visual Designer (for PowerBASIC Windows 10+)
    Version 3 now available.
    http://www.planetsquires.com

  • #2
    Try this, might be able to modify it to suit your needs.

    http://www.powerbasic.com/support/pb...ad.php?t=23778
    Last edited by George Bleck; 11 Oct 2008, 07:45 PM.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

    Comment


    • #3
      Thanks George - I'll take a look.
      Paul Squires
      FireFly Visual Designer (for PowerBASIC Windows 10+)
      Version 3 now available.
      http://www.planetsquires.com

      Comment


      • #4
        GIVEN:

        S() sorted array which looks like...

        C:
        C:\DOG
        C:\DOG\CAT
        C:\DOG\CAT2
        D:
        D:\DOG
        D:\DOG\CAT
        D:\DOG\CAT2

        Code:
        LOCAL hParentITem () AS LONG 
        
        REDIM hParentItem(50)   ' enough to hold every possible subfolder  
        
        FOR Z = LBOUND (S,1) TO UBOUND (S,1)  ' for each Drive/Folder in S() 
        
          ' is it a drive? 
            IF RIGHT$(S(Z) = ":" THEN 
              iLevel =  0               ' root 
            ELSE
             ' find the 'depth' or 'level' of this node: 
               iLevel = TALLY(s(Z), "\")
            END IF 
            hParentITem(iLevel) =  TV_INSERTITEM (Parent= IIF&(iLevel=0, %TV_ROOT, hParentItem(iLevel-1))) 
            END IF 
        NEXT
        Needs some tweaking but it's a start. The key is keeping track of the last "hitem" by "level" ("depth") and the array thing has always worked for me.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Thanks guys,

          I was able to build some code that works really well. The core code that does the work is shown below. Granted, it is pretty application specific so you won't be able to simply cut and paste it into an application.

          The general idea is to iterate through the sorted array list. Whenever the path of the string changes then we need to create a new parent node. The trick is to ensure that each piece of the path already has a parent node that exists. To do that check I look up the piece of the path in a Hash Table. The hash table entry holds the peice of the path and its associated treeview node handle. Holding the data in a hash table makes processing the strings extremely fast. I have posted the hash table code elsewhere in these forums.

          Code:
             Array Sort sFiles(), TagArray nIndex(), Collate UCase
          
             ' Create the hash table
             pDict = hash_create( 100, %FALSE )
             
             For y = 1 To nCount
                ' If the text has changed then we need to add a new parent node.
                If ( y = 1) Or (y > 1 And sFiles( y - 1 ) <> sFiles( y )) Then
                   ' Determine under which node to put this node. We start by defaulting to
                   ' placing it under the root node (hNodeFunction). 
                   hParentNode = hNodeFunction 
          
                   ' Walk the string ensuring that each section of the path 
                   ' exists in the Treeview (i.e. it should have the node value
                   ' saved in the hash and is returned via the zData parameter).
                   NumSections = ParseCount( sFiles(y), "\" )
                   For z = 1 To NumSections
                       st = Parse$(sFiles(y), "\", z)
                       ' If the section exists in the Hash then update the hParentNode
                       ' that we will use when adding the path to the Treeview.
                       If Hash_Find( pDict, st, zData ) Then
                          hParentNode = zData
                       Else
                          hParentNode = FF_TreeView_InsertItem( tv, hParentNode, st, %NODE_CATEGORY, _  
                                                                    g.Explorer_ClosedIcon, g.Explorer_OpenIcon )   
                          Hash_Add pDict, st, hParentNode
                       End If
                   Next
                   
                End If
                
                CodeItem = gclsCodestore.GetCodeItem(nIndex(y))
                FF_TreeView_InsertItem tv, hParentNode, CodeItem.FunctionName, 0, _
                                            g.Explorer_CodeIcon, g.Explorer_CodeIcon    
             Next
             
             hash_destroy pDict
          Paul Squires
          FireFly Visual Designer (for PowerBASIC Windows 10+)
          Version 3 now available.
          http://www.planetsquires.com

          Comment


          • #6
            Your Key/Data approach is imaginative.

            I like imaginative.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              I found that using IDictionary is faster than Paul's hash table. I was quite surprised.

              There is a test piece attached to one of my replies in this thread.

              http://www.powerbasic.com/support/pb...t=38402&page=2

              James

              Comment

              Working...
              X