Add, AddItem Method (TreeView, Nodes)

Purpose

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

Syntax

TreeView.Add[Item]( relative, relationship, key, text, image, selectedimage)

Nodes.Add(relative, relationship, key, text, image, selectedimage)

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

Description

The TreeView Ocx has the AddItem and Add methods, which act exactly the same. The Nodes object supports the Add method only.

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.
relationship Optional. Specifies the relative placement of the Node object:
tvwFirst (0) First. The Node is placed before all other nodes at the same level of the node named in relative.
tvwLast (1) Last. 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.
tvwNext (2) (Default) Next. The Node is placed after the node named in relative.
tvwPrevious (3) Previous. The Node is placed before the node named in relative.
tvwChild (4) Child. The Node becomes a child node of the node named in relative.
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.

Use the Key property to reference a member of the Nodes collection if you expect the value of an object's Index property to change, such as by dynamically adding objects to or removing objects from the collection. The Nodes collection is a 1-based collection.

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 Add method returns a reference to the newly created Node object, it is most convenient to set properties of the new Node using this reference. The following example adds several Node objects with identical properties:

Example

Dim node As Node

Ocx TreeView tv = "", 10, 10, 100, 200

Set node = tv.Add( , tvwChild, "David" , "David")

Set node = tv.Add(1, tvwChild, "Peter", "Peter")

Set node = tv.Add("David", tvwChild, "Angela", "Angela")

Set node = tv.Add( , , "Arthur", "Arthur")

tv.Item("Peter").EnsureVisible ' Expand tree to see all nodes.

Do : Sleep : Until Me Is Nothing

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: 23/09/2014 by James Gaite}