Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

TreeView Walkthrough in just 15 lines of code - PBWin9

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gary Beene
    replied
    And without being too Perl-ish about it, I was just looking at the earlier version and realized I could take out another 2 lines and a variable declaration.

    Here's the revised version. Now we're talking about minimal code - only 11 lines of code if you don't count the SUB/DIM/End Sub statements!

    Code:
    Sub TreeWalk2(ByVal hWnd As Dword, ByVal hNode As Dword)
       Dim iReturn 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 hNode            'parent
                   Treeview Get Next hWnd, %ID_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)
            Sleep 250 : If iReturn Then Treeview Select hWnd, %ID_TreeView, hNode
       Loop While hNode
    End Sub
    I posted a thread in the Programming forum that has more description of how the code works.

    Leave a comment:


  • TreeView Walkthrough in just 15 lines of code - PBWin9

    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).

    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
    It gives this kind of walkthrough.

    Code:
    1
      2
         3
         4 
         5
      6 
         7
         8 
         9 
     10 
         11
            12
            13
    Here's a compilable version to play with.

    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
Working...
X
😀
🥰
🤢
😎
😡
👍
👎