Functions for inserting items

There are several functions for adding items to a TreeView control, as shown in Table 8-2.

Table 8-2: Functions for adding items to TreeView control

This function

Adds an item here

InsertItem

After a sibling item for the specified parent.

If no siblings exist, you must use one of the other insertion functions.

InsertItemFirst

First child of the parent item.

InsertItemLast

Last child of the parent item.

InsertItemSort

As a child of the parent item in alphabetic order, if possible.

For all the InsertItem functions, the SortType property can also affect the position of the added item.

There are two ways to supply information about the item you add, depending on the item properties that need to be set.

Method 1: specifying the label and picture index only

You can add an item by supplying the picture index and label. All the other properties of the item will have default values. You can set additional properties later as needed, using the item’s handle.

Example This example inserts a new item after the currently selected item on the same level as that item. First it gets the handles of the currently selected item and its parent, and then it inserts an item labeled Hindemith after the currently selected item. The item’s picture index is 2:

long ll_tvi, ll_tvparent

ll_tvi = tv_list.FindItem(CurrentTreeItem!, 0)
ll_tvparent = tv_list.FindItem(ParentTreeItem!, &
   ll_tvi)
tv_list.InsertItem(ll_tvparent, ll_tvi, &
   "Hindemith", 2)

Method 2: setting item properties in a TreeViewItem structure

You can add items by supplying a TreeViewItem structure with properties set to specific values. The only required property is a label. Properties you might set are shown in Table 8-3.

Table 8-3: TreeViewItem properties

Property

Value

Label

The text that is displayed for the item.

PictureIndex

A value from the regular picture list.

SelectedPictureIndex

A value from the regular picture list, specifying a picture that is displayed only when the item is selected. If 0, no picture is displayed for the item when selected.

StatePictureIndex

A value from the State picture list. The picture is displayed to the left of the regular picture.

Children

Must be TRUE if you want double-clicking to trigger the ItemPopulate event. That event script can insert child items.

Data

An optional value of any datatype that you want to associate with the item. You might use the value to control sorting or to make a database query.

Example This example sets all these properties in a TreeViewItem structure before adding the item to the TreeView control. The item is inserted as a child of the current item:

treeviewitem tvi
long h_item = 0, h_parent = 0

h_parent = tv_1.FindItem(CurrentTreeItem!, 0)
tvi.Label = "Choral"
tvi.PictureIndex = 1
tvi.SelectedPictureIndex = 2
tvi.Children = true
tvi.StatePictureIndex = 0
h_item = tv_1.InsertItemSort(h_parent, tvi)

For more information about inserting items into a TreeView control, see the PowerScript Reference.