Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLoad a TreeView control from a CSV file in VB .NET
DescriptionThis example shows how to load a TreeView control from a CSV (comma-separated value) file in Visual Basic .NET.
KeywordsTreeView, CSV
CategoriesControls, Files and Directories, VB.NET
 
This example reads the whole file into a string and uses the string's Split method to break it into lines.

It breaks each line into fields using the commas as delimters and calls subroutine MakeTreeViewPath to make the appropriate TreeView node.

 
' Load a TreeView control from a CSV file.
Private Sub LoadTreeViewFromCsvFile(ByVal file_name As _
    String, ByVal trv As TreeView)
    ' Load the CSV file.
    Dim stream_reader As New StreamReader(file_name)
    Dim file_contents As String = stream_reader.ReadToEnd()
    stream_reader.Close()

    ' Break the file into lines.
    Const charCR As Char = CChar(vbCr)
    Const charTab As Char = CChar(vbTab)
    Dim lines() As String = file_contents.Split(charCR)

    ' Process the lines.
    trv.Nodes.Clear()
    Dim new_fields() As String
    For i As Integer = 0 To lines.GetUpperBound(0)
        ' Make sure the line isn't blank.
        If lines(i).TrimStart.Length > 0 Then
            ' Break the line into fields.
            new_fields = lines(i).Trim.Split(","c)

            ' Make the entry.
            MakeTreeViewPath(trv.Nodes, new_fields, 0)
        End If
    Next i
End Sub
 
Subroutine MakeTreeViewPath searches a particular level in the TreeView control to see if a node at that level has the needed text for this part of a path. If it finds such a node, MakeTreeViewPath recursively calls itself to find the next field for the path within that TreeView node's children.

If it doesn't find a child with the needed text, subroutine MakeTreeViewPath makes a new child.

 
' Make a TreeView node using this CSV path.
Private Sub MakeTreeViewPath(ByVal parent_nodes As _
    TreeNodeCollection, ByVal fields() As String, ByVal _
    field_num As Integer)
    ' Do nothing if we've used all of the fields.
    If field_num > fields.GetUpperBound(0) Then Exit Sub

    ' Search the parent_nodes for a child with this
    ' field as its name.
    Dim found_field As Boolean
    For Each child_node As TreeNode In parent_nodes
        If child_node.Text = fields(field_num) Then
            ' We found this field. Follow it.
            MakeTreeViewPath(child_node.Nodes, fields, _
                field_num + 1)
            found_field = True
        End If
    Next child_node

    ' See if we found the field.
    If Not found_field Then
        ' We didn't find the field. Make it here.
        Dim new_node As TreeNode = _
            parent_nodes.Add(fields(field_num))
        new_node.EnsureVisible()

        ' Make the rest of the path.
        MakeTreeViewPath(new_node.Nodes, fields, field_num _
            + 1)
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated