Advertisement
ASP_Volume3 Files/ File Controls/ Input/ Output #45352

Load and Save TreeView's to/from a text file

These are two functions I wrote to save and load a treeview's nodes (saves the .text, .tag, and .key properties) to and from a text file. This is a very simple code and should be very easy to incorporate to any project.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Public Sub LoadTree(ByVal tvTree As TreeView, ByVal sFileName As String)
   
' Function by Chetan Sarva (November 17, 1999)
' Please include this comment if you use this code.
Dim curNode As Node
Dim sDelimiter As String
Dim freef As Integer
Dim buf As String
Dim nodeparts As Variant
sDelimiter = "" ' We want something extremely unique to delimit
        ' each of the pices of our treeview
        
 On Error Resume Next
 
 ' Get a free file and open our file for output
 freef = FreeFile()
 Open sFileName For Input As #freef
 
  Do
  DoEvents
  
   ' Read in the current line
   Line Input #freef, buf
   ' Split the line into pieces on our delimiter
   nodeparts = Split(buf, sDelimiter)
   
   ' See if it's a root or child node and add accordingly
   If nodeparts(3) = "parent" Then
    curNode = tvTree.Nodes.Add(, , nodeparts(1), nodeparts(0))
    curNode.Tag = nodeparts(2)
   Else
    curNode = tvTree.Nodes.Add(nodeparts(3), tvwChild, nodeparts(1), nodeparts(0))
    curNode.Tag = nodeparts(2)
   End If
   
  Loop Until EOF(freef)
  
 Close #freef
 
End Sub
Public Sub SaveTree(ByVal tvTree As TreeView, ByVal sFileName As String)
        
' Function by Chetan Sarva (November 17, 1999)
' Please include this comment if you use this code.
Dim curNode As Node
Dim sDelimiter As String
Dim freef As Integer
sDelimiter = "" ' We want something extremely unique to delimit
        ' each of the pices of our treeview
 On Error Resume Next
 
 ' Get a free file and open our file for output
 freef = FreeFile()
 Open sFileName For Output As #freef
 
  ' Loop through all the nodes and save all the
  ' important information
  For Each curNode In tvTree.Nodes
   
   If curNode.FullPath = curNode.Text Then
    Print #freef, curNode.Text; sDelimiter; curNode.Key; sDelimiter; curNode.Tag; sDelimiter; "parent"
   Else
    Print #freef, curNode.Text; sDelimiter; curNode.Key; sDelimiter; curNode.Tag; sDelimiter; curNode.Parent.Key
   End If
   
  Next curNode
  
 Close #freef
 
End Sub

Upload
To download Visual Studio 2005 Express Edition visit Microsoft at:
http://msdn.microsoft.com/vstudio/express/default.aspx
Original Comments (3)
Recovered from Wayback Machine