• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

在treeview中从一个文件保存并加载项目

发布: 2007-6-17 21:38 | 作者:   | 来源:   | 查看: 51次 | 进入软件测试论坛讨论

领测软件测试网

   
  Save and load the contents of a TreeView to and from a file

How do you save and load the contents of a TreeView to and from a file? I注释:ve been asked this question several times and I注释:ve also seen it pop up a few times on the VB-World Q and A forums so I thought it would be useful here in the tips library.

This code makes use of the Contents property of a PropertyBag object. This property was introduced in VB6 so VB5 users can注释:t use these routines I注释:m afraid.

What it does

The code consists of two public Sub routines called SaveTree and LoadTree. The SaveTree procedure takes 3 arguments:

The file name (including the path) to where you want the file to be saved. The TreeView control of which contents you want to save.
The root node: The node you want the save to begin with. You can pass any valid node and the procedure will save the contents from that node and below.
The SaveTree procedure calls a private Sub called FillPropBag which is doing the actual job of filling the PropertyBag that is saved. It does this in a recursive manner.
The LoadTree procedure takes two arguments: the file name you want to load and The TreeView that you want to populate.

Copy the following code into a BAS module or even better add the code to a Class module and change the code so the FileName and TreeView arguments are properties instead.

Have Fun!
Joacim Andersson (joacim@programmer.net)

注释:-------------------------------
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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网