Managing TreeView items

An item in a TreeView is a TreeViewItem structure. The preceding section described how to set the item’s properties in the structure and then insert it into the TreeView.

This code declares a TreeViewItem structure and sets several properties:

TreeViewItem tvi_defined

tvi_defined.Label = "Symphony No. 3 Eroica"
tvi_defined.StatePictureIndex = 0
tvi_defined.PictureIndex = 3
tvi_defined.SelectedPictureIndex = 4
tvi_defined.OverlayPictureIndex = 0
tvi_defined.Children = TRUE

For information about Picture properties, see “Managing TreeView pictures”.

When you insert an item, the inserting function returns a handle to that item. The TreeViewItem structure is copied to the TreeView control, and you no longer have access to the item’s properties:

itemhandle = This.InsertItemLast(parenthandle, &
   tvi_defined)

Procedure for items: get, change, and set

If you want to change the properties of an item in the TreeView, you:

  1. Get the item, which assigns it to a TreeViewItem structure.

  2. Make the changes, by setting TreeViewItem properties.

  3. Set the item, which copies it back into the TreeView.

When you work with items that have been inserted in the TreeView, you work with item handles. Most TreeView events pass one or two handles as arguments. The handles identify the items the user is interacting with.

This code for the Clicked event uses the handle of the clicked item to copy it into a TreeViewItem structure whose property values you can change:

treeviewitem tvi
This.GetItem(handle, tvi)
tvi.OverlayPictureIndex = 1
This.SetItem(handle, tvi)

NoteImportant Remember to call the SetItem function after you change an item’s property value. Otherwise, nothing happens in the TreeView.

Items and the hierarchy

You can use item handles with the FindItem function to navigate the TreeView and uncover its structure. The item’s properties tell you what its level is, but not which item is its parent. The FindItem function does:

long h_parent
h_parent = This.FindItem(ParentTreeItem!, handle)

You can use FindItem to find the children of an item or to navigate through visible items regardless of level.

For more information, see the FindItem function in the PowerScript Reference.

Enabling TreeView functionality in scripts

By setting TreeView properties, you can enable or disable user actions like deleting or renaming items without writing any scripts. You can also enable these actions by calling functions. You can: