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 from a text file with tabs denoting indentation
KeywordsTreeView, text file, load
CategoriesControls, Files and Directories
 
Read the file and count the leading tabs. Give the new node the right parent for its level of indentation.
 
' Load a TreeView control from a file that uses tabs
' to show indentation.
Private Sub LoadTreeViewFromFile(ByVal file_name As String, _
    ByVal trv As TreeView)
Dim fnum As Integer
Dim text_line As String
Dim level As Integer
Dim tree_nodes() As Node
Dim num_nodes As Integer

    fnum = FreeFile
    Open file_name For Input As fnum

    TreeView1.Nodes.Clear
    Do While Not EOF(fnum)
        ' Get a line.
        Line Input #fnum, text_line

        ' Find the level of indentation.
        level = 1
        Do While Left$(text_line, 1) = vbTab
            level = level + 1
            text_line = Mid$(text_line, 2)
        Loop

        ' Make room for the new node.
        If level > num_nodes Then
            num_nodes = level
            ReDim Preserve tree_nodes(1 To num_nodes)
        End If

        ' Add the new node.
        If level = 1 Then
            Set tree_nodes(level) = TreeView1.Nodes.Add(, , _
                , text_line)
        Else
            Set tree_nodes(level) = _
                TreeView1.Nodes.Add(tree_nodes(level - 1), _
                tvwChild, , text_line)
            tree_nodes(level).EnsureVisible
        End If
    Loop

    Close fnum
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated