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 :-)
Printable View
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 :-)
nested for-loops......
Actually the function would have to be recursive since you have no idea how many loops you would need.
well, do you know before hand how many levels in the tree goes?
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.
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?
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
thanks,
i ll try these.
This isn't working for me. Nodes are saved correctly, but their relationships aren't.
this is working perfect, just be shure the nodes are unique :-)