Date: 02-16-2022
Return to Index
created by gbSnippets
'The TreeView message %TVM_SortChildren has an option to sort all children, but
'I've been unable to make this work:
Control Send hDlg, %IDC_TreeView, %tvm_sortchildren, %True, hNode 'children + sub-nodes
'So, this version uses a backup approach, of cycling through all parents and sorting
'their immediately children with the following code, which does work:
Control Send hDlg, %IDC_TreeView, %tvm_sortchildren, %False, hNode 'children only
'Given a node in the TreeView, this snippet walks through the rest of the Tree and
'sorts all nodes it finds. Start at the root to sort the entire tree.
'Primary Code - Expand Tree
Sub SortNodes(ByVal hNode as DWord)
Local iReturn As DWord, hStartNode as DWord
hStartNode = hNode
Do
Control Send hDlg, %IDC_TreeView, %tvm_sortchildren, %False, hNode 'children only
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 '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 = hStartNode Then iReturn = 0 : Exit Loop 'if reach starting node, stop
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)
Loop While hNode
End Sub
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%IDC_TreeView = 100 : %IDC_Button = 200
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_Button,"Start", 25,220,95,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
Local hNode As DWord
If CB.Msg = %WM_Command AND CB.Ctl = %IDC_Button Then
TreeView Get Select hDlg, %IDC_TreeView To hNode
SortNodes(hNode) 'sort children + sub-nodes
End If
End Function
Sub SortNodes(ByVal hNode as DWord)
Local iReturn As DWord, hStartNode as DWord
hStartNode = hNode
Do
Control Send hDlg, %IDC_TreeView, %tvm_sortchildren, %False, hNode 'children only
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 '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 = hStartNode Then iReturn = 0 : Exit Loop 'if reach starting node, stop
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)
Loop While hNode
End Sub
'gbs_00271
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm