AddFirst, AddLast, AddNext, AddPrev, AddChild Methods (Nodes)

Purpose

Add a Node to a Nodes collection in a TreeView control and returns a reference to the newly created Node object.

Syntax

Nodes.AddFirst(relative, key, text, image, selectedimage)

Nodes.AddLast(relative, key, text, image, selectedimage)

Nodes.AddNext(relative, key, text, image, selectedimage)

Nodes.AddPrev(relative, key, text, image, selectedimage)

Nodes.AddChild(relative, key, text, image, selectedimage)

relative, key, text, image, selectedimage: Variant exp

Description

The Nodes object supports the Add method to add a Node to the collection; however the basic Add method requires the relationship with the relative node to be specified, so rather than using the general Add object, GFA-BASIC 32 offers a series of methods that implicitly includes that relationship. These are:

AddFirst The Node is placed before all other nodes at the same level of the node named in relative.
AddLast The Node is placed after all other nodes at the same level of the node named in relative. Any Node added subsequently may be placed after one added as Last.
AddNext The Node is placed after the node named in relative.
AddPrev The Node is placed before the node named in relative.
AddChild The Node becomes a child node of the node named in relative.

The arguments of the methods are:

relative Optional. The index number or key of a pre-existing Node object. The relationship between the new node and this pre-existing node is found in the next argument, relationship.
key Optional. A unique string that can be used to retrieve the Node with the Item method.
text Required. The string that appears in the Node.
image Optional. The index of an image in an associated ImageList control.
selectedimage Optional. The index of an image in an associated ImageList control that is shown when the Node is selected.

As a Node object is added it is assigned an index number, which is stored in the Node object's Index property. This value of the newest member is the value of the Node collection's Count property.

Because the Addxx methods return a reference to the newly created Node object, you can set properties of the new Node using this reference.

Example

Dim n As Node, nd As Nodes

Ocx TreeView tv = "", 0, 0, 200, 300

.Style = tvwTreelinesPlusMinusText  : .LineStyle = tvwRootLines

Set n = tv.AddItem( , , "Bert" , "Bert")                // Node 1

Set nd = tv.Nodes

// Below are numerous ways to add a Child...

Set n = nd.AddChild("Bert" , "Harry" , "Harry")         // Node 2

Set n = tv.Nodes.AddChild("Bert", "Charlie", "Charlie") // Node 3

Set n = tv.Nodes.Add(2, tvwChild , "Mary" , "Mary")     // Node 4

nd.AddChild "Mary", "Bertha" , "Bertha"                 // Node 5

// Set n = nd.AddFirst("Bert", "Gerald" , "Gerald") doesn't work

/* Use the following instead:

nd.Add "Bert", tvwFirst, , "Gerald" ' or Set n = nd.Add("Bert", tvwFirst, , "Gerald")

// Similarly, AddLast, AddNext, AddPrevious do not appear to work either

/* Instead use: nd.Add [Relative], [Type], [Key], [Text]

tv.Node(5).EnsureVisible

Do : Sleep : Until Me Is Nothing

Known Issues

As shown in the example above, the AddFirst, AddLast, AddNext and AddPrev methods all trigger the 'Invalid Property Value' error report regardless of what values are entered; AddChild doesn't seem to be affected by this problem.

Remarks

GFA-BASIC 32 specific

Instead of explicitly using the Nodes collection to access a Node element, you can use a shorter notation. First, the TreeView supports an Item property:

tv.Item(idx)tv.Nodes.Item(idx)

Like the Item method of tv.Nodes, Item is the default method of TreeView. Therefore, a Node can be accessed as follows:

tv(idx)tv.Nodes(idx)

tv!idxtv.Nodes!idx

Each dot saves about 30 bytes of code.

To enumerate over the Nodes collection of a TreeView Ocx, use For Each on the Ocx control directly, like:

Local node1 As Node

For Each node1 In tv : DoSomething(node1) : Next

See Also

TreeView, Node, Nodes

{Created by Sjouke Hamstra; Last updated: 09/06/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}