[Gambas-user] Newb needs help, Could I have an example of treefile save?

Stephen Bungay sbungay at ...981...
Sat Mar 22 04:57:31 CET 2008


   When last I looked, TreeView.Parent does not store the parent key of 
the current node, but rather points you to the parent container for the 
treeview control. As such TreeView.Parent is, for the purpose of storing 
the tree structure, fairly useless.
   You can use a collection and a class to store the tree structure. If 
the tree itself can not give you the salient details you need to persist 
it's structure to a table or text file, then it falls on you to create 
the mechanism to do the job.
   A class with three fields (lets call it ClassTree) can store what you 
need.

Key
ParentKey
NodeText

   When you add a node to the tree you already know
1. What key you added or is to be added
2. What node text is going to be used
3. What the parent key is for the node being added

The time to capture this information is when you add a new node. You can 
do something like this...

TreeClass = new ClassTree
With TreeClass
      .Key = KeyBeingAdded
      .ParentKey = ParentKey
      .NodeText = TextString
end with

TreeCollection.Add TreeClass

   When it comes time to persist the tree to a file you simply walk the 
TreeCollection by using a for-next loop. Your code might look something 
like this...

open "myfile" as #1
For X = 1 to TreeCollection.COunt
     With TreeCollection
          WRITE #1 .Key, .ParentKey, .NodeText
     End With
Next
close #1

   Of course you can store the name of an image file (don't forget the 
path to the file) as well. Just add the ImageName to the class, so your 
class would look like this;

Key
ParentKey
NodeText
ImageName


   and adding it to the collection then looks something like this;

TreeClass = new ClassTree
With TreeClass
      .Key = KeyBeingAdded
      .ParentKey = ParentKey
      .NodeText = TextString
      .ImageName = ImageName
end with

TreeCollection.Add TreeClass

   Hope this helps.

Steve.

jbskaggs wrote:
> As yall know I am just beginning programming, and In my program I am at the
> point of where I need to save my treeview.  But I am getting confused by the
> save commands.  
> 
> Could you show me an example of how exactly to open and save file that would
> record each treeview.item's characteristics:
> 
> ie  this is what I am thinking but it's not working and 
> 
>   open "myfile" as #1
> 
>   treecount = TreeView2.Count
> 
>   REPEAT 
>     WRITE #1 TreeView2.Item.Key, TreeView2.Item.Image, TreeView2.Item.Text,
> TreeView2.Parent
>     treeview2.MoveNext
>     savecount+=1
>   UNTIL savecount = treecount
> 
> so could somebody give the code to do this?
> 
> Thanks
> 
> JB SKaggs





More information about the User mailing list