Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_TreeView = 500
%IDC_TextBoxIn = 501
%IDC_TextBoxOut = 502
%IDC_ButtonBuild = 503
%IDC_ButtonGet = 504
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "TreeView",200,200,590,250, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, %IDC_ButtonBuild, "Build TreeView", 10,5,125,25
Control Add Button, hDlg, %IDC_ButtonGet, "Build Text From TreeView", 400,5,150,25
Control Add TextBox, hDlg, %IDC_TextBoxIn,"", 10,40,180,200, %ES_MultiLine Or %ES_WantReturn
Control Add Treeview, hDlg, %IDC_TreeView,"", 200,40,190,200
Control Add TextBox, hDlg, %IDC_TextBoxOut,"", 400,40,180,200, %ES_MultiLine Or %ES_WantReturn
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$
Select Case Cb.Msg
Case %WM_InitDialog
Control Set Text hDlg, %IDC_TextBoxIn, _
"City" + $CrLf + _
"...Dallas" + $CrLf + _
"...Chicago" + $CrLf + _
"...New York" + $CrLf + _
"State" + $CrLf + _
"...Texas" + $CrLf + _
"......Cowboys" + $CrLf + _
"......Cowgirls" + $CrLf + _
"...New York" + $CrLf + _
"Idaho"
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_ButtonBuild
Control Get Text hDlg, %IDC_TextBoxIn To temp$
BuildTreeViewCode(temp$,1,1)
Case %IDC_ButtonGet
Control Set Text hDlg, %IDC_TextBoxOut, GetTreeViewContent
End Select
End Select
End Function
Function BuildTreeViewCode(TVContent As String, ShowRoot As Long, ExpandAll As Long) As String
Local record$, caption$, recordnext$, hItem, hTemp As Dword, i,DepthCurrent, DepthNext As Long
ReDim hParent(100) As Dword, Parent(100) As String
'add root node if required
If ShowRoot Then Treeview Insert Item hDlg, %IDC_TreeView, 0, %TVI_Last, 0, 0, "Root" To hParent(0)
'build interior
For i = 1 To ParseCount(TVcontent,$CrLf)
'Get info about Current and Next records
record$ = Parse$(TVcontent,$CrLf,i)
depthcurrent = ParseCount(record$,"...")
caption$ = LTrim$(record$,".")
recordnext$ = Parse$(TVContent,$CrLf,i+1)
depthnext = ParseCount(recordnext$,"...")
'put current record into TreeView
If DepthNext > DepthCurrent Then 'is parent node
Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hItem 'add parent
hParent(DepthCurrent) = hItem 'save parent node value
Else 'is child node
Treeview Insert Item hDlg, %IDC_TreeView, hParent(depthcurrent-1), %TVI_Last, 0,0,caption$ To hTemp 'add child
If ExpandAll Then Treeview Set Expanded hDlg, %IDC_TreeView, hParent(depthcurrent-1), 1 'expand parent
End If
Next i
End Function
Function GetTreeViewContent() As String
Local iReturn,hNode As Dword, temp$,tmp$, D As Long
Treeview Get Root hDlg, %IDC_TreeView To hNode
Do
Treeview Get Child hDlg, %IDC_TreeView, hNode To iReturn 'get child (1st choice)
If iReturn = 0 Then Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn Else Incr D '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
If hNode Then Decr D
Treeview Get Next hDlg, %IDC_TreeView, hNode To iReturn 'sibling child of parent
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)
Treeview Get Text hDlg, %IDC_TreeView, hNode To tmp$ : temp$ = temp$ + $CrLf + Repeat$(D-1,"...") + tmp$
Loop While hNode
Function = Trim$(temp$,$CrLf)
End Function
'gbs_01259
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm