You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Announcement
Collapse
No announcement yet.
TreeView Walkthrough in just 15 lines of code - PBWin9
Actually, I would be interested in hearing from anyone who uses the code. I tried a few versions of tree structures and it seemed to work for them all, but I'd enjoy hearing how it worked from someone else.
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
That is of course, if no one finds a Tree it won't work on.
For those of you needing more comments than those I put in the code, here's how it works.
Starting at any node, it looks down the tree for a child. If no child if found, then it looks for a sibling. Following a child gets precedence over following a sibling.
If no child or sibling exists, it goes up the tree looking for the first parent which has a sibling. When the bottom of the tree is reached, there are no more parents with siblings.
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!
Not to be a noodge but my editor counts 13 lines (12 wo the Sleep 250)
[quote]
Code:
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
==================================
"What we are today
comes from thoughts of yesterday,
and our present thoughts
build our life of tomorrow:
our life is the creation
of our minds."
Buddha
==================================
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment