Node, Nodes Objects (TreeView)

Purpose

A Node object is an item in a TreeView control that can contain images and text.

A Nodes object is a collection of Node objects.

Syntax

TreeView.Nodes

TreeView.Nodes[.Item](index)

index : Variant

Description

The syntax above refers to the collection and to individual elements in the collection, respectively, according to the standard collection syntax.

The TreeView.Nodes property returns a reference to the Nodes object, a collection of Node objects.

TreeView.Nodes.Item(index) returns a reference to the Node with the given index (integer or string). Since Item is the default property it can be left out.

For each Node object, you can add text and pictures. However, to use pictures, you must reference an ImageList control using the ImageList property of the TreeView Ocx.

Pictures can change depending on the state of the node; for example, a selected node can have a different picture from an unselected node if you set the SelectedImage property to an image from the associated ImageList.

You can manipulate Node objects using standard collection methods (for example, the Add and Remove methods). Each element in the collection can be accessed by its index, the value of the Index property, or by a unique key, the value of the Key property.

The Nodes collection properties and methods:

Add | AddFirst | AddLast | AddNext | AddPrev |AddChild | Clear | Count | Item | Remove

The Node properties and methods:

BackColor | Bold | Child | Children | CreateDragImage | EnsureVisible | Expanded | ExpandedImage | FirstSibling | ForeColor | FullPath | Index | Image | Italic | Key | LastSibling | Next | Parent | Previous | Root | Selected | SelectedImage | Sorted | Tag | Text | TreeViewName | Underline | Visible

Node only properties:

% = Children Returns the number of child Node objects contained in a Node object.
CreateDragImage Not implemented

Example

Dim node As Node

Ocx TreeView tv = "", 250, 10, 230, 200

tv.Add , , , "Painters"

tv.Nodes.Add  1, tvwChild , , "Da Vinci"

tv.Add 1, tvwChild, , "Titian"

tv.AddItem 1, tvwChild, , "Rembrandt"

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

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

tv.LineStyle = tvwRootLines

tv.Style = tvwTreelinesText

tv.Indentation = 25

tv("David").Italic = True

tv.Node(3).Bold = True

tv.Nodes(4).Underline = True

tv!David.EnsureVisible ' Expand tree to see all nodes.

tv.SetFocus

tv("David").Selected = 1

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 nod As Node

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

See Also

ListView

{Created by Sjouke Hamstra; Last updated: 22/10/2017 by James Gaite}