Date: 02-16-2022
Return to Index
created by gbSnippets
'Once you have a Tree, this code will save the content to a text file,
'and also restore it into the tree. User data is also captured.
'Primary Code:
'There are 2 routines, one to save, one to restore.
'Because of length, the Save/Restore code is only shown
'once at the bottom of the Compilable Example listing.
Sub TreeCreateFromFile(sFileName$, HideRoot&)
' ... see below
End Sub
Sub TreeSaveToFile(sFileName$) 'root information always saved
' ... see below
End Sub
'The format of the saved file is as follows, with 3 lines for each
'node: Level, User value, Node text. These are separated by a delimiter.
'Chr$("++", "--", ";;") is used, but almost any delimiter would do.
' 0 'Level 0-root
' 0 'User value
'Root 'Node text
'****** 'delimiter - Chr$("++", "--", ";;")
' 1
' 0
'Mother
'****** 'delimiter - Chr$("++", "--", ";;")
' 2
' 0
'Dan
'****** 'delimiter - Chr$("++", "--", ";;")
'2
'0
'Bob
'See the TreeCreateFromFile sub below to see how a Tree is restored
'from this format. Basically, each node is a child of the first lower
'level node above it in the listing.
'Compilable Example: (Jose Includes)
'This examples also allows the optional display of the Root node.
'(some folks like to see the root item, some don't)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_TreeView = 100 : %IDC_ButtonS = 200 : %IDC_ButtonC = 300 : %IDC_ButtonR = 400
Global hDlg As DWord
Function PBMain() As Long
Local hItem As DWord, hTemp As DWord, hTemp2 As DWord, hTemp3 As DWord
Dialog New Pixels, 0, "TreeView",200,200,155,250, %WS_SysMenu, 0 To hDlg
Control Add TreeView, hDlg, %IDC_TreeView, "", 10,10,130,200
Control Add Button, hDlg, %IDC_ButtonS,"Save", 10,220,40,20
Control Add Button, hDlg, %IDC_ButtonC,"Create", 60,220,40,20
Control Add Button, hDlg, %IDC_ButtonR,"Reset", 110,220,40,20
TreeView Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0,0,"Root" To hItem
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Mother" To hTemp
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Dan" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Bob" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Foot" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Arm" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Insert Item hDlg, %IDC_TreeView, hItem, %TVI_Last, 0,0,"Father" To hTemp
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Helen" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp, %TVI_Last, 0,0,"Any" To hTemp3
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp, %True
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Leg" To hTemp2
TreeView Insert Item hDlg, %IDC_TreeView, hTemp3, %TVI_Last, 0,0,"Finger" To hTemp2
TreeView Set Expanded hDlg, %IDC_TreeView, hTemp3, %True
TreeView Set Expanded hDlg, %IDC_TreeView, hItem, %True
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Dim hTreeView As DWord, hNode As DWord
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_ButtonS Then
TreeSaveToFile Exe.Path$ + "mytree.txt"
End If
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_ButtonC Then
TreeCreateFromFile Exe.Path$ + "mytree.txt", %True
End If
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_ButtonR Then
TreeView Reset hDlg, %IDC_TreeView
End If
End Function
Sub TreeCreateFromFile(sFileName$, HideRoot&)
Local temp$, i As Long, hTemp As DWord, iLevel As Long
Local LevelHandle() As Long, D$, TreeData() As String, iUser&
Dim LevelHandle(-1 To 100)
D$ = "++" + "--" + ";;" + $CrLf
'Clear anything already in the TreeView control
TreeView Reset hDlg, %IDC_TreeView
'put all data into array TreeData()
Open sFileName$ For Binary As #1
Get$ #1, Lof(1), temp$
Close #1
Dim TreeData(ParseCount(temp$, D$)-1)
Parse temp$, TreeData(), D$
'Handle the Root
iLevel = Val(Parse$(TreeData(0), $CrLf, 1)) 'level
iUser = Val(Parse$(TreeData(0), $CrLf, 2)) 'user value
TreeData(0) = Parse$(TreeData(0),$CrLf,3) 'text content
If HideRoot& = 0 Then
TreeView Insert Item hDlg, %IDC_TreeView, LevelHandle(iLevel-1), %TVI_Last, 0,0, TreeData(0) To hTemp
End If
LevelHandle(0) = hTemp
'all other nodes - remember, all start with $crlf + D$
For i = 1 To UBound(TreeData)
iLevel = Val(Parse$(TreeData(i), $CrLf, 1)) 'level
iUser = Val(Parse$(TreeData(i), $CrLf, 2)) 'user value
TreeData(i) = Parse$(TreeData(i),$CrLf,3) 'text content
TreeView Insert Item hDlg, %IDC_TreeView, LevelHandle(iLevel-1), %TVI_Last, 0,0, TreeData(i) To hTemp
LevelHandle(iLevel) = hTemp
Next i
End Sub
Sub TreeSaveToFile(sFileName$)
Local iLevel As Long, temp$, iUser&, iReturn As DWord, hNode As DWord, LevelHandle() As Long, D$
Dim LevelHandle(-1 To 100)
D$ = "++" + "--" + ";;"
'Open file for output
Open sFileName$ For Output As #1
'get root node info from tree (top of tree)
TreeView Get Root hDlg, %IDC_TreeView To hNode
iLevel = 0
TreeView Get User hDlg, %IDC_TreeView, hNode To iUser&
TreeView Get Text hDlg, %IDC_TreeView, hNode To temp$
Print #1, Str$(iLevel) + $CrLf + Str$(iUser&) + $CrLf + temp$
LevelHandle(0) = hNode
'walk through rest of tree
Do
TreeView Get Child hDlg, %IDC_TreeView, hNode To iReturn 'get child (1st choice)
If iReturn Then Incr iLevel 'new level
If iReturn = 0 Then TreeView Get Next hDlg, %IDC_TreeView, hNode To iReturn 'or sibling (2nd choice)
If iReturn = 0 Then 'no child or sibling
Do 'get sibling of first parent with sibling
TreeView Get Parent hDlg, %IDC_TreeView, hNode To hNode 'parent
TreeView Get Next hDlg, %IDC_TreeView, hNode To iReturn 'sibling child of parent
Decr iLevel 'dropped one level
Loop Until iReturn Or (hNode = 0) 'stop when find sibling of parent with sibling, or no more choices
End If
hNode = iReturn 'possible values: 0, zero (no parent/no sibling), <>0 (parent or sibling)
'get info from tree
If hNode Then
TreeView Get User hDlg, %IDC_TreeView, hNode To iUser&
TreeView Get Text hDlg, %IDC_TreeView, hNode To temp$
Print #1, D$ + $CrLf + Str$(iLevel) + $CrLf + Str$(iUser&) + $CrLf + temp$
LevelHandle(iLevel) = hNode
End If
Loop While hNode
Close #1
End Sub
'gbs_00269
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm