[Gambas-user] Could someone give me a brief explanation of parents and kids in this example

B Bruen bbruen at ...2308...
Thu May 15 01:35:09 CEST 2014


On Wed, 14 May 2014 10:57:58 -0500
Jb Skaggs <jbskaggs42 at ...626...> wrote:

> In the treeview example I would like to understand the parent and kids aspects:
> 
> Public Sub Button1_Click()
> 
>   Dim sIcon As String
>   Dim sParent As String
> 
>   If Textbox1.Text <> Null Then
>     If RadioButton1.Value Then
>       sIcon = "Male.png"
>     Else
>       sIcon = "Female.png"
>     End If
>     'Gets the parent item: the current item, or nothing is the treeview is void
>     sParent = TreeView1.Key
>     'Now we will add a new entry with a key and a name of what was in
> the text box
>     'We will place it as a child of the currently selected entry
>     TreeView1.Add(Textbox1.Text, Textbox1.Text, Picture[sIcon],
> sParent).EnsureVisible
>     TreeView1.Item.EnsureVisible 'This will make sure that the item we
> just added to the list is in the visable area of the control.
> (Scrolling if necessary)
>     TextBox1.Text = "" 'This empties out textbox
>     RefreshInfo ' This will update our label and reflect the new number of kids 
> End
> 
> 
> My understanding was that a kid or child was a copy or instant of the
> parent but I don't know if this is true in Gambas-  so I would like to
> understand what actually happens when something is a parent and child.
>  Thank you. Jb Skaggs
> 
If we just stick to the treeview for the moment (which I believe is the crux here), and firstly focus on your 

    TreeView1.Add(Textbox1.Text, Textbox1.Text, Picture[sIcon], sParent)

line. 

What does this TreeView.Add(...) method do? 

It creates an object which is an instance of the _Treeview_Item class and returns it. A _Treeview_Item is a "node" in the tree. Its' existence though, depends on the actual TreeView object concerned. In other words its identity, as specified by the value of its "Key" property , is unique for that Treeview object and provides the means to access the node via the myTreeView[skeyvalue] mechanism.  All the other properties of the node may be considered either "descriptive" or "display-directing".
Now the importance of all that is that each node in a tree is entirely independent.  There is no inherent relationship within the data structure regarding its' parents or children.

Que? What about this ParentKey parameter/property?

The ParentKey is just "descriptive" to us humans and "display-directing" to the treeview control. If it is null then the control draws it as a "root node", if it is non-null then the control assumes that a node exists with that key and attempts to draw it as a child of that node. (Of course, if that parent node does not exist then an error occurs).

So each node is distinct. They are neither copies nor instances of some "parent" object.

If we look at the node.Children property in the same light, it just returns the number of nodes that will be drawn as direct children of the current node. It knows nothing about them, neither their entities (instances) nor their identities (keys).

So, a node (in a gambas treeview object) is a _Treeview_Item which is just a set of drawing instructions that the treeview control follows to display itself, enhanced with a few more "descriptive" properties that we can use to do stuff in code.

On the other hand, if we are talking about OO inheritance here, then that is an entirely different kettle of fish which has nothing to do with treeview controls.

hth

-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list