[Gambas-user] How to read keys from a TreeView

Benoit Minisini gambas at ...1...
Mon Jan 12 13:53:44 CET 2009


On lundi 12 janvier 2009, Rolf-Werner Eilert wrote:
> Proceeding to convert a Gambas1 app to Gambas2, I found that reading key
> values or stepping through the values is different now.
>
> The TreeView shows names of persons sorted under groups/classes/filters
> sorted under years sorted under a root header, in brief:
>
> Root1
> 	2004
> 	2005
> 	2006
> 		Group1
> 		Group2 <-- Cursor
> 			John
> 			Benoit
> 			Michael
> 			Rolf
> 		Group3
> 	2007
> 	2008
> Root2
>
> What I want is when the cursor is on Group2 and I call my function, it
> expands and reads all keys of the names within this group.
>
> This is the code in Gambas1:
>
>       baum.Current.Expanded = TRUE
>
>       namenKeys.Clear
>
>       FOR i = 1 TO baum.Current.Children
>         baum.Item.MoveBelow
>         IF Left$(baum.Item.Key, 1) = "#" THEN
>           namenKeys.Add(baum.Item.Key)
>         ELSE
>           BREAK
>         END IF
>       NEXT
>
> "baum" is the TreeView, "namenKeys" is an array that should return the
> keys which were found. The idea was to read the keys either up to the
> number of children or until the next key doesn't have a "#".
>
> This doesn't run in Gambas2 this way, but I didn't find a way to make it
> run. MoveBelow doesn't seem to exist anymore (Gambas2 won't show an
> error, however), and I tried any possible combination of using
> MoveAfter() with Item and Current, but to no avail.
>
> So, can anybody help here?
>
> Thanks so much!
>
> Rolf
>

MoveBelow() is a method of TreeView in Gambas 2, and moves the internal cursor 
one item below. (In Gambas 1, this method did not work in all cases).

But as you want the child items of a node, you must not use MoveBelow(). It 
moves one item below, whether it is a child or not of the initial parent 
item. 

So if one of the child has children too, you routine won't work.

You must use MoveNext() instead:

  baum.Current.Expanded = TRUE

  namenKeys.Clear

  ' Move the internal cursor to the current item
  baum.MoveCurrent()
  ' Then move to the first child of the current item
  baum.MoveChild()

  ' While the internal cursor is valid, i.e. while there is
  ' a child to look at.
  WHILE baum.Available

    IF Left$(baum.Item.Key, 1) = "#" THEN
      namenKeys.Add(baum.Item.Key)
    ELSE
      BREAK
    END IF
    
    ' Move to the next sibling item, i.e. the next item having 
    ' the same parent.
    baum.MoveNext()

  WEND

See the documentation of the methods in the wiki to know what they are doing 
exactly.

Regards,

-- 
Benoit Minisini




More information about the User mailing list