Results 1 to 10 of 10

Thread: Saving contents of a treeview into a textfile

  1. #1

    Thread Starter
    Lively Member ranma_at's Avatar
    Join Date
    Oct 2002
    Location
    @ Home
    Posts
    72

    Lightbulb Saving contents of a treeview into a textfile

    Hi,

    has anyone a codesnippet for storing a treeview with all its nodes and childs into a textfile, somehow sorted so the nodes dont get lost?

    thanks :-)
    Things are not happening to you, things are happening because of you!

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    nested for-loops......

  3. #3
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Actually the function would have to be recursive since you have no idea how many loops you would need.
    Please rate my post.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    well, do you know before hand how many levels in the tree goes?

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    I'm not sure if you're asking me or not, but if you are: No I don't know and that's why I suggest a recursive function to save. Going this route would make the number of nests in the treeview irrelevant.
    Please rate my post.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    I havent used the treeview in some years... But wasn't there a property that told you how many levels were underneath a specific node?

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The easiest way would be to create a UDT that represents a record in your file. This udt holds the data you want to save for each node. Make sure you include the Node.Key and the Node.Parent.Key.

    Then just loop through all the nodes, no recursive function is neccessary, and write out the data. Loading the tree simply becomes a matter of running through the file, creating a node for every record.

    VB Code:
    1. Private Type NodeType
    2.     ParentKey As String * 20
    3.     Key As String * 20
    4.     Text As String * 50
    5.     Bold As Boolean
    6.     Color As Long
    7. End Type
    8.  
    9.  
    10. Private Sub SaveTreeToFile()
    11.     Dim lngFile As Long
    12.     Dim objNode As Node
    13.     Dim udtNodeData As NodeType
    14.    
    15.     'just overwrite the existing file
    16.     lngFile = FreeFile
    17.     Open "D:\Testing\Treeview.dat" For Binary As lngFile
    18.    
    19.     For Each objNode In Treeview1.Nodes
    20.        
    21.         If Not objNode.Parent Is Nothing Then
    22.             udtNodeData.ParentKey = objNode.Parent.Key
    23.         Else
    24.             udtNodeData.ParentKey = ""
    25.         End If
    26.        
    27.         udtNodeData.Key = objNode.Key
    28.         udtNodeData.Text = objNode.Text
    29.         udtNodeData.Bold = objNode.Bold
    30.         udtNodeData.Color = objNode.ForeColor
    31.        
    32.         Put lngFile, , udtNodeData
    33.     Next
    34.    
    35.     Close lngFile
    36. End Sub
    37.  
    38. Private Sub LoadTreeFromFile()
    39.     Dim lngFile As Long
    40.     Dim objNode As Node
    41.     Dim udtNodeData As NodeType
    42.    
    43.     Dim strParent As String
    44.     Dim strKey As String
    45.    
    46.     With Treeview1
    47.         .Visible = False
    48.         .Nodes.Clear
    49.         .Visible = True
    50.     End With
    51.  
    52.     lngFile = FreeFile
    53.     Open "D:\Testing\TreeView.dat" For Binary As lngFile
    54.    
    55.     Get lngFile, , udtNodeData
    56.    
    57.     Do
    58.         strParent = udtNodeData.ParentKey
    59.         strKey = udtNodeData.Key
    60.        
    61.         If Len(Trim$(strParent)) > 0 Then
    62.             Set objNode = Treeview1.Nodes.Add(Trim$(strParent), tvwChild, Trim$(strKey))
    63.         Else
    64.             Set objNode = Treeview1est.Nodes.Add(, , Trim$(strKey))
    65.         End If
    66.        
    67.         objNode.Bold = udtNodeData.Bold
    68.         objNode.Text = udtNodeData.Text
    69.         objNode.ForeColor = udtNodeData.Color
    70.        
    71.         Get lngFile, , udtNodeData
    72.     Loop Until EOF(lngFile)
    73.    
    74.     Close lngFile
    75.  
    76. End Sub

  8. #8

    Thread Starter
    Lively Member ranma_at's Avatar
    Join Date
    Oct 2002
    Location
    @ Home
    Posts
    72
    thanks,
    i ll try these.
    Things are not happening to you, things are happening because of you!

  9. #9
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    This isn't working for me. Nodes are saved correctly, but their relationships aren't.
    Please rate my post.

  10. #10

    Thread Starter
    Lively Member ranma_at's Avatar
    Join Date
    Oct 2002
    Location
    @ Home
    Posts
    72

    Talking

    this is working perfect, just be shure the nodes are unique :-)
    Things are not happening to you, things are happening because of you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width