|
-
Aug 16th, 2000, 03:47 AM
#1
Thread Starter
New Member
How can I save the content in a treeview control to a file and..? I also need to know how to get the treeview's content from the file into the control again... Anyone knows how to do this??
-
Aug 16th, 2000, 06:35 AM
#2
If you have VB6 you could fill a PropertyBag object and write down the Contents property of the PropertyBag to a file.
I written a Sub called SaveTree that takes three arguments:
A file name, the TreeView control which contents you want to save and the root node of that tree.
The SaveTree sub calles an other sub I called FillPropBag which goes through the tree in a recursive manner.
The LoadTree sub takes two arguments:
The file name and the TreeView control you want to populate.
Here's the code:
Code:
Private Sub FillPropBag(pb As PropertyBag, tvw As MSComctlLib.TreeView, Root As MSComctlLib.Node)
Static nCount As Long
Dim nde As MSComctlLib.Node
Dim nIndex As Long
If pb Is Nothing Then
Set pb = New PropertyBag
nCount = 0
pb.WriteProperty "NumberOfNodes", tvw.Nodes.Count, 0
End If
On Error Resume Next
pb.WriteProperty "Text" & nCount, Root.Text
nIndex = Root.Parent.Index
If nCount = 0 Then
nIndex = 0
End If
pb.WriteProperty "Parent" & nCount, nIndex, 0
pb.WriteProperty "Key" & nCount, Root.Key, ""
pb.WriteProperty "Index" & nCount, Root.Index, 0
pb.WriteProperty "Image" & nCount, Root.Image, vbEmpty
pb.WriteProperty "SelectedImage" & nCount, Root.SelectedImage, vbEmpty
Set nde = Root.Child
If nde Is Nothing Then
Set nde = Root.Next
If nde Is Nothing Then
Exit Sub
End If
End If
nCount = nCount + 1
FillPropBag pb, tvw, nde
End Sub
Public Sub SaveTree(sFilename As String, tvw As MSComctlLib.TreeView, Root As MSComctlLib.Node)
Dim pb As PropertyBag
Dim bArr() As Byte
Dim iFile As Integer
FillPropBag pb, tvw, Root
iFile = FreeFile
Open sFilename For Binary Access Write As #iFile
bArr = pb.Contents
Put #iFile, , bArr
Close #iFile
End Sub
Public Sub LoadTree(sFilename As String, tvw As MSComctlLib.TreeView)
Dim pb As PropertyBag
Dim bArr() As Byte
Dim iFile As Integer
Dim nde As MSComctlLib.Node
Dim nCount As Long
Dim nNumOfNodes As Long
Dim nIndex As Long
Dim sText$, sKey$
Dim nParent As Long
Dim vImage, vSelectedImage
iFile = FreeFile
Open sFilename For Binary Access Read As #iFile
ReDim bArr(LOF(iFile)) As Byte
Get #iFile, , bArr
Close #iFile
Set pb = New PropertyBag
pb.Contents = bArr
tvw.Nodes.Clear
nNumOfNodes = pb.ReadProperty("NumberOfNodes", 0)
If nNumOfNodes = 0 Then
Exit Sub
End If
Do While nCount < nNumOfNodes
nParent = pb.ReadProperty("Parent" & nCount, 0)
nIndex = pb.ReadProperty("Index" & nCount, 0)
sKey = pb.ReadProperty("Key" & nCount, "")
sText = pb.ReadProperty("Text" & nCount, "")
vImage = pb.ReadProperty("Image" & nCount, vbEmpty)
vSelectedImage = pb.ReadProperty("SelectedImage" & nCount, vbEmpty)
If nParent Then
tvw.Nodes.Add tvw.Nodes(nParent), tvwChild, sKey, sText, vImage, vSelectedImage
Else
tvw.Nodes.Add , , sKey, sText, vImage, vSelectedImage
End If
nCount = nCount + 1
Loop
End Sub
Good luck!
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
|