# TvwNodes class
{: .no_toc }

The collection of all the nodes of a [**TreeView**](/tB/Packages/Contributed/VBCCR18/TreeView/index.md), which its [**Nodes**](/tB/Packages/Contributed/VBCCR18/TreeView/index.md#nodes) property returns: the nodes of every level, not those of one branch. Its items are [**TvwNode**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md) objects, numbered from 1, and a node can have a key as well.

```tb check_build check_run arch=win32,win64
Dim Node As TvwNode
Set Node = TreeView1.Nodes.Add(, , "usa", "United States")
TreeView1.Nodes.Add Node, TvwNodeRelationshipChild, "nyc", "New York"
TreeView1.Nodes.Add , , "fra", "France"
For Each Node In TreeView1.Nodes
    Debug.Print Node.Index; Node.Text
Next
' Output:
'  1 United States
'  2 New York
'  3 France
```

A program goes through the nodes with **For Each**, or by number from 1 to [**Count**](#count). To go through the nodes in the order of the tree, it follows the [**Child**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#child) and [**NextSibling**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#nextsibling) properties of the nodes. It cannot make a **TvwNodes** object with **New**.

* TOC
{:toc}

## Numbers and keys

The number of a node is its place in this collection, and the collection holds the nodes in the order they were added. A node that [**Add**](#add) puts between two others still has the next number, so the numbers do not follow the order in which the tree shows the nodes. A number changes when [**Remove**](#remove) takes a node with a smaller number out, and a key stays with its node.

A key is any string, and capital and small letters in it are the same: `"Fruit"` and `"FRUIT"` are one key. No two nodes of a tree have the same key. A node needs no key: an empty string is no key, and any number of nodes can be without one.

```tb check_build check_run arch=win32,win64
Dim Node As TvwNode
Set Node = TreeView1.Nodes.Add(, , "x", "X")
Set Node = TreeView1.Nodes.Add(, , "y", "Y")
Set Node = TreeView1.Nodes.Add("x", TvwNodeRelationshipNext, "z", "Z")
Debug.Print TreeView1.Nodes("Z").Index
Debug.Print TreeView1.Nodes("x").NextSibling.Text
Debug.Print TreeView1.Nodes(2).Text
' Output:
'  3
'  Z
'  Y
```

Properties
----------

### Count
{: .no_toc }

The number of nodes in the tree. **Long**, read-only.

### Item
{: .no_toc }

Returns one node. It is the default member, so `TreeView1.Nodes("docs")` is `TreeView1.Nodes.Item("docs")`.

Syntax: *object*.**Item**( *Index* ) **As TvwNode**

*Index*
: *required* The number of the node, from 1 to [**Count**](#count), or its key as a **String**.

A number outside that range raises run-time error 9, and a key that no node has raises run-time error 5. [**Exists**](#exists) asks without an error.

Methods
-------

### Add
{: .no_toc }

Puts a node into the tree and returns its [**TvwNode**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md).

Syntax: *object*.**Add**( [ *Relative* ] [, *Relationship* ] [, *Key* ] [, *Text* ] [, *Image* ] [, *SelectedImage* ] ) **As TvwNode**

*Relative*
: *optional* The node that the new one is placed by: a **TvwNode**, or the number or the key of one. Without it the new node goes to the root level.

*Relationship*
: *optional* A member of [**TvwNodeRelationshipConstants**](/tB/Packages/Contributed/VBCCR18/TreeView/index.md#tvwnoderelationshipconstants): where the new node goes in relation to *Relative*. The default is **TvwNodeRelationshipNext**.

*Key*
: *optional* A **String** by which the program can name the node. See [Numbers and keys](#numbers-and-keys).

*Text*
: *optional* A **String**: the label of the node. The default is an empty string.

*Image*, *SelectedImage*
: *optional* The picture the node shows, and the picture it shows while it is selected: the number of a picture of the tree's image list, or its key as a **String**. See [Pictures](/tB/Packages/Contributed/VBCCR18/TreeView/index.md#pictures). Without them the node has no picture of its own.

Without a *Relative* the relationship is counted from the root level: **TvwNodeRelationshipFirst** puts the node before every other node of the root level, and every other relationship except **TvwNodeRelationshipPrevious** puts it after the last one.

> [!WARNING] 1.8.141.0
> A call with **TvwNodeRelationshipPrevious** and no *Relative* never returns. A program that places a node before another names that node as the *Relative*.

A *Relative* that is a key no node has raises run-time error 5, a number outside the range of nodes raises 9, and a value that is not a node, a string or a whole number raises 13. A *Key* that another node has raises run-time error 457. A call that raises an error adds no node.

A picture is named by its number or, when the tree has an **ImageList** control, by its key. A key the list does not have raises run-time error 35601, and a negative number raises 35600. A number is not checked against the size of the list.

While the tree's [**Sorted**](/tB/Packages/Contributed/VBCCR18/TreeView/index.md#sorted) is **True**, a node that goes to the root level takes its sorted place, whatever the relationship says. So does a child added to a parent whose [**Sorted**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#sorted) is **True**.

### Clear
{: .no_toc }

Removes every node. A [**TvwNode**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md) that the program still holds refers to no node after that.

Syntax: *object*.**Clear**

### Exists
{: .no_toc }

Returns whether the tree has a node of a number or a key, so that a program can ask before [**Item**](#item) raises an error.

Syntax: *object*.**Exists**( *Index* ) **As Boolean**

*Index*
: *required* The number or the key to look for.

### Remove
{: .no_toc }

Removes one node, and with it all the nodes below it. The numbers of the nodes that follow move down to fill the gap.

Syntax: *object*.**Remove** *Index*

*Index*
: *required* The number or the key of the node. One that the tree does not have raises the error that [**Item**](#item) raises for it.

A **TvwNode** that the program holds for a removed node stays valid but refers to nothing: its [**Text**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#text) is an empty string, its [**Parent**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#parent) is **Nothing** and its [**Children**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#children) is 0. Its [**Index**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#index) and [**Key**](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md#key) keep the old values.

## See Also

- [TreeView](/tB/Packages/Contributed/VBCCR18/TreeView/index.md) class
- [TvwNode](/tB/Packages/Contributed/VBCCR18/TreeView/TvwNode.md) class -- one node
- [TvwSelectedNodes](/tB/Packages/Contributed/VBCCR18/TreeView/TvwSelectedNodes.md) class -- the selected nodes of a tree that allows several
