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 text file, let the user edit it, and save it into a text file in VB 6
DescriptionThis example shows how to load a TreeView control from a text file, let the user edit it, and save it into a text file in Visual Basic 6. The text file uses tabs to indicate an item's depth of indentation in the file. To save the file, the code recursively traverses the TreeView's nodes.
KeywordsTreeView, load, save, edit
CategoriesControls, Files and Directories
 
Subroutine LoadTreeViewFromFile reads the file and counts the leading tabs. It gives each new node the right parent for its level of indentation.
 
Option Explicit
' 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
    If trv.Nodes.Count > 0 Then trv.Nodes(1).EnsureVisible
End Sub
 
When the user presses the right mouse button down on the TreeView, the code selects the node under the mouse and displays a popup menu. The mnuPopupAddNode menu item propmts the user for text and then creates a new node with that text. The mnuPopupDeleteNode menu item deletes the selected text.
 
Private Sub TreeView1_MouseDown(Button As Integer, Shift As _
    Integer, x As Single, y As Single)
    If Button = vbRightButton Then
        Set TreeView1.SelectedItem = TreeView1.HitTest(x, y)
        PopupMenu mnuPopup
    End If
End Sub

Private Sub mnuPopupAddNode_Click()
Dim txt As String
Dim new_node As Node

    txt = InputBox("Text", "Add Node", "")
    If Len(txt) > 0 Then
        If TreeView1.SelectedItem Is Nothing Then
            Set new_node = TreeView1.Nodes.Add(, , , txt)
        Else
            Set new_node = TreeView1.Nodes.Add( _
                TreeView1.SelectedItem, tvwChild, , txt)
        End If
        new_node.EnsureVisible
    End If
End Sub

Private Sub mnuPopupDeleteNode_Click()
    TreeView1.Nodes.Remove TreeView1.SelectedItem.Index
End Sub
 
Subroutine SaveTreeViewIntoFile calls subroutine SaveNode passing it the TreeView control's first node.

Subroutine SaveNode writes a node's text into a file with the right number of tabs in front to indicate the node's level in the tree. It then calls itself recursively to save the node's first child (that call recursively saves the node's entire subtree). It then calls itself recursively again passing itself the node's next sibling node (that saves everything that comes after this node's subtree).

 
' Save a TreeView control into a file that uses tabs
' to show indentation.
Private Sub SaveTreeViewIntoFile(ByVal file_name As String, _
    ByVal trv As TreeView)
Dim fnum As Integer

    fnum = FreeFile
    Open file_name For Output As fnum

    ' Find the root nodes.
    If TreeView1.Nodes.Count > 0 Then SaveNode fnum, _
        TreeView1.Nodes(1), 0

    Close fnum
End Sub

' Write tabs indicating this node's depth in
' the tree followed by the node's text.
' Then save its children and its siblings.
Private Sub SaveNode(ByVal fnum As Integer, ByVal n As _
    Node, ByVal level As Integer)
    If n Is Nothing Then Exit Sub

    ' Save the node.
    Print #fnum, String$(level, vbTab) & n.Text

    ' Save its children.
    SaveNode fnum, n.Child, level + 1

    ' Save its next sibling.
    SaveNode fnum, n.Next, level
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated