|
-
Jun 18th, 2003, 12:39 PM
#1
Thread Starter
Lively Member
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!
-
Jun 18th, 2003, 12:46 PM
#2
Hyperactive Member
-
Jun 18th, 2003, 01:24 PM
#3
Frenzied Member
Actually the function would have to be recursive since you have no idea how many loops you would need.
-
Jun 18th, 2003, 01:58 PM
#4
Hyperactive Member
well, do you know before hand how many levels in the tree goes?
-
Jun 18th, 2003, 02:07 PM
#5
Frenzied Member
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.
-
Jun 18th, 2003, 02:12 PM
#6
Hyperactive Member
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?
-
Jun 18th, 2003, 02:32 PM
#7
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:
Private Type NodeType
ParentKey As String * 20
Key As String * 20
Text As String * 50
Bold As Boolean
Color As Long
End Type
Private Sub SaveTreeToFile()
Dim lngFile As Long
Dim objNode As Node
Dim udtNodeData As NodeType
'just overwrite the existing file
lngFile = FreeFile
Open "D:\Testing\Treeview.dat" For Binary As lngFile
For Each objNode In Treeview1.Nodes
If Not objNode.Parent Is Nothing Then
udtNodeData.ParentKey = objNode.Parent.Key
Else
udtNodeData.ParentKey = ""
End If
udtNodeData.Key = objNode.Key
udtNodeData.Text = objNode.Text
udtNodeData.Bold = objNode.Bold
udtNodeData.Color = objNode.ForeColor
Put lngFile, , udtNodeData
Next
Close lngFile
End Sub
Private Sub LoadTreeFromFile()
Dim lngFile As Long
Dim objNode As Node
Dim udtNodeData As NodeType
Dim strParent As String
Dim strKey As String
With Treeview1
.Visible = False
.Nodes.Clear
.Visible = True
End With
lngFile = FreeFile
Open "D:\Testing\TreeView.dat" For Binary As lngFile
Get lngFile, , udtNodeData
Do
strParent = udtNodeData.ParentKey
strKey = udtNodeData.Key
If Len(Trim$(strParent)) > 0 Then
Set objNode = Treeview1.Nodes.Add(Trim$(strParent), tvwChild, Trim$(strKey))
Else
Set objNode = Treeview1est.Nodes.Add(, , Trim$(strKey))
End If
objNode.Bold = udtNodeData.Bold
objNode.Text = udtNodeData.Text
objNode.ForeColor = udtNodeData.Color
Get lngFile, , udtNodeData
Loop Until EOF(lngFile)
Close lngFile
End Sub
-
Jun 18th, 2003, 11:28 PM
#8
Thread Starter
Lively Member
Things are not happening to you, things are happening because of you!
-
Jun 19th, 2003, 12:33 AM
#9
Frenzied Member
This isn't working for me. Nodes are saved correctly, but their relationships aren't.
-
Jun 20th, 2003, 11:51 AM
#10
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|