Results 1 to 3 of 3

Thread: [RESOLVED] Data Tree View help!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Resolved [RESOLVED] Data Tree View help!

    This is somewhat of a multi part question, let me start off by explaining what I'm trying to do. I have an organized folder system with data files in them. Each folder has a set structure to their naming, and can contain sub folders and files. It will be a jagged directory system. I'm trying to import their structure into a TreeView.

    I have the workings of a recursive system that adds all files and folders into a ViewTree, however they aren't organized by parent/child. So I get this...
    -C:\Temp
    -C:\Temp\Data
    -C:\Temp\file1.txt
    -C:\Temp\file2.txt
    -C:\Temp\Data\file3.txt
    -C:\Temp\Data\file4.txt
    etc........


    How would I go about parsing the directory paths, and include them into a parent/child relationship... looking like this
    +Temp
    --file1.txt
    --file2.txt
    -+Data
    ---file3.txt
    ---file4.txt


    Also I when the line item is selected, I need to know which file is selected so I can use it's full path to call up data (using an API) based upon the it's full root path.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data Tree View help!

    You may need to refine this a little but it should get you well and truly on the way:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.AddNode("C:\Temp")
            Me.AddNode("C:\Temp\Data")
            Me.AddNode("C:\Temp\file1.txt")
            Me.AddNode("C:\Temp\file2.txt")
            Me.AddNode("C:\Temp\Data\file3.txt")
            Me.AddNode("C:\Temp\Data\file4.txt")
        End Sub
    
        Private Sub AddNode(ByVal path As String)
            Dim labels As New Stack(Of String)
    
            Do Until IO.Path.GetPathRoot(path) = path
                labels.Push(IO.Path.GetFileName(path))
                path = IO.Path.GetDirectoryName(path)
            Loop
    
            Dim nodes As TreeNodeCollection = Me.TreeView1.Nodes
            Dim node As TreeNode
            Dim label As String
    
            While labels.Count > 0
                label = labels.Pop()
    
                If nodes.ContainsKey(label) Then
                    node = nodes(label)
                Else
                    node = nodes.Add(label, label)
                End If
    
                nodes = node.Nodes
            End While
        End Sub
    
    End Class
    Note that that AddNode method will actually add all parent nodes automatically, so you don't need these lines:
    Code:
    Me.AddNode("C:\Temp")
    Me.AddNode("C:\Temp\Data")
    That said, you may then have to manipulate the ordering to get the subfolders listed before the files. I'll let you mess with that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Data Tree View help!

    Works Perfectly! Didnt know about the "push", "pop" thing. Need to look at those more closely!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width