VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



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

by Chetan Sarva (5 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

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.

Inputs
No inputs...
Assumes
None needed...
Code Returns
Doesn't return anything...
Side Effects
No side effects...
API Declarations
No API's used here...

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

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

Download this snippet    Add to My Saved Code

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

No comments have been posted about Load and Save TreeView's to/from a text file. Why not be the first to post a comment about Load and Save TreeView's to/from a text file.

Post your comment

Subject:
Message:
0/1000 characters