In an earlier thread, I gave a ~25 line code for a traversing a TreeView control. While it worked, it wasn't "satisfying". Plus, jcfuller hurt my feelings by not embracing the code - kind of like a mother who is hurt when someone says her baby is ugly! (ok, he didn't really hurt my feelings, but he did say my baby was ugly!).
It got me to thinking about a better solution. Here's the result - just 15 lines of code. It fixes some of the objections I heard last time - there are no globals (do equates count as globals?), no arrays, and no recursive calls. It starts at any node and does not have to traverse the whole tree (it can be stopped at any node).
It gives this kind of walkthrough.
Here's a compilable version to play with.
It got me to thinking about a better solution. Here's the result - just 15 lines of code. It fixes some of the objections I heard last time - there are no globals (do equates count as globals?), no arrays, and no recursive calls. It starts at any node and does not have to traverse the whole tree (it can be stopped at any node).
Code:
Sub TreeWalk(ByVal hWnd As Dword, ByVal hNode As Dword) Dim iReturn As Dword, hParent As Dword, hSibling As Dword Do Treeview Get Child hWnd, %ID_Treeview, hNode To iReturn 'get child (1st choice) If iReturn = 0 Then Treeview Get Next hWnd, 100, 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 hWnd, %ID_TreeView, hNode To hParent 'parent Treeview Get Next hWnd, %ID_TreeView, hParent To hSibling 'child of parent hNode = hParent Loop Until hSibling Or (hParent = 0) 'stop when find parent with sibling (or no more choices) If hSibling Then iReturn = hSibling 'otherwise iReturn remains zero End If hNode = iReturn Sleep 250 : If iReturn Then Treeview Select hWnd, %ID_TreeView, hNode Loop While hNode End Sub
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
Code:
#Compile Exe #Dim All %ID_TreeView = 100 Function PBMain() As Long Local hDlg As Dword, hItem As Dword Local 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, 100, "", 10,10,130,200 Treeview Insert Item hDlg, 100, 0, %TVI_Last, 2,2,"Top" To hItem Treeview Insert Item hDlg, 100, hItem, %TVI_Last, 2,4,"Mother" To hTemp Treeview Insert Item hDlg, 100, hTemp, %TVI_Last, 2,4,"Dan" To hTemp2 Treeview Insert Item hDlg, 100, hTemp, %TVI_Last, 1,4,"Bob" To hTemp3 Treeview Insert Item hDlg, 100, hTemp3, %TVI_Last, 2,4,"Foot" To hTemp2 Treeview Insert Item hDlg, 100, hTemp3, %TVI_Last, 1,4,"Arm" To hTemp2 Treeview Insert Item hDlg, 100, hItem, %TVI_Last, 1,4,"Father" To hTemp Treeview Insert Item hDlg, 100, hTemp, %TVI_Last, 2,4,"Helen" To hTemp2 Treeview Insert Item hDlg, 100, hTemp, %TVI_Last, 1,4,"Any" To hTemp3 Treeview Insert Item hDlg, 100, hTemp3, %TVI_Last, 2,4,"Leg" To hTemp2 Treeview Insert Item hDlg, 100, hTemp3, %TVI_Last, 1,4,"Finger" To hTemp2 Control Add Button, hDlg, 200, "Walk", 10,220,40,20 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 = 200 Then Treeview Get Select Cb.Hndl, 100 To hNode TreeWalk Cb.Hndl, hNode End If End Function Sub TreeWalk(ByVal hWnd As Dword, ByVal hNode As Dword) Dim iReturn As Dword, hParent As Dword, hSibling As Dword Do Treeview Get Child hWnd, %ID_Treeview, hNode To iReturn 'get child (1st choice) If iReturn = 0 Then Treeview Get Next hWnd, 100, 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 hWnd, %ID_TreeView, hNode To hParent 'parent Treeview Get Next hWnd, %ID_TreeView, hParent To hSibling 'child of parent hNode = hParent Loop Until hSibling Or (hParent = 0) 'stop when find parent with sibling (or no more choices) If hSibling Then iReturn = hSibling 'otherwise iReturn remains zero End If hNode = iReturn Sleep 250 : If iReturn Then Treeview Select hWnd, %ID_TreeView, hNode Loop While hNode End Sub
Comment