Populating TreeViews
You must write a script to add items to a TreeView. You cannot
add items in the painter as with other list controls. Although you
can populate all the levels of the TreeView at once, TreeView events
allow you to populate only branches the user looks at, which saves
unnecessary processing.
Typically, you populate the first level of the TreeView when
the control is displayed. This code might be in a window’s
Open event, a user event triggered from the Open event, or the TreeView’s
Constructor event. Then a script for the control’s ItemPopulate
event would insert an item’s children when the user chooses
to expand it.
The ItemPopulate event is triggered when the user clicks on
an item’s plus button or double-clicks the item, but only
if the item’s Children property is TRUE.
Therefore, when you insert an item that will have children, you
must set its Children property to TRUE so that
it can be populated with child items when the user expands it.
You are not restricted to adding items in the ItemPopulate
event. For example, you might let the user insert items by dragging
from a ListBox or filling in a text box.
Functions for inserting items
There are several functions for adding items to a TreeView
control, as shown in Table 8-2.
| 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:
|
1 |
long ll_tvi, ll_tvparent<br /><br />ll_tvi = tv_list.FindItem(CurrentTreeItem!, 0)<br />ll_tvparent = tv_list.FindItem(ParentTreeItem!, &<br /> ll_tvi)<br />tv_list.<i>InsertItem</i>(ll_tvparent, ll_tvi, &<br /> "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.
| 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:
|
1 |
treeviewitem tvi<br />long h_item = 0, h_parent = 0<br /><br />h_parent = tv_1.FindItem(CurrentTreeItem!, 0)<br />tvi.Label = "Choral"<br />tvi.PictureIndex = 1<br />tvi.SelectedPictureIndex = 2<br />tvi.Children = true<br />tvi.StatePictureIndex = 0<br />h_item = tv_1.InsertItemSort(h_parent, tvi) |
For more information about inserting items
into a TreeView control, see the PowerScript Reference
.
Inserting items at the root level
The very first item you insert does not have any sibling for
specifying a relative position, so you cannot use the InsertItem function–you
must use InsertItemFirst or InsertItemLast.
For an item inserted at the root level, you specify 0 as its parent.
This sample code is in a user event triggered from the Open
event of the window containing the TreeView. It assumes two instance
variable arrays:
- A string array called item_label that
contains labels for all the items that will be inserted at the root
level (here composer names) - An integer array that has values for the Data property
(the century for each composer); the century value is for user-defined
sorting:1int ct<br />long h_item = 0<br />treeviewitem tvi<br /><br />FOR ct = 1 TO UpperBound(item_label)<br /> tvi.Label = item_label[ct]<br /> tvi.Data = item_data[ct]<br /> tvi.PictureIndex = 1<br /> tvi.SelectedPictureIndex = 2<br /> tvi.Children = TRUE<br /> tvi.StatePictureIndex = 0<br /> tvi.DropHighlighted = TRUE<br /> h_item = tv_1.InsertItemSort(0, tvi)<br />NEXT
After inserting all the items, this code scrolls the TreeView
back to the top and makes the first item current:
|
1 |
// Scroll back to top<br />h_item = tv_1.FindItem(RootTreeItem!, 0)<br />tv_1.SetFirstVisible(h_item)<br />tv_1.SelectItem(h_item) |
Inserting items below the root level
The first time a user tries to expand an item to see its children, PowerBuilder triggers
the ItemPopulate event if and only if the value
of the item’s Children property is TRUE.
In the ItemPopulate event, you can add child items for the item
being expanded.
Parent item’s Children property If the ItemPopulate event does not occur when you expect,
make sure the Children property for the expanding item is TRUE.
It should be set to TRUE for any item that will
have children.
Inserting items not restricted to the ItemPopulate
event The ItemPopulate event helps you design an efficient program.
It will not populate an item that the user never looks at. However,
you do not have to wait until the user wants to view an item’s
children. You can add children in any script, just as you added
items at the root level.
For example, you might fully populate a small TreeView when
its window opens and use the ExpandAll function
to display its items fully expanded.
Has an item been populated? You can check an item’s ExpandedOnce property to
find out if the user has looked at the item’s children.
If the user is currently looking at an item’s children,
the Expanded property is also TRUE.
Example This TreeView lists composers and their music organized into categories.
The script for its ItemPopulate event checks whether the item being expanded
is at level 1 (a composer) or level 2 (a category). Level 3 items
are not expandable.
For a level 1 item, the script adds three standard categories.
For a level 2 item, it adds pieces of music to the category being
expanded, in this pattern:
|
1 |
Mozart<br /> Orchestral<br /> Symphony No. 33<br /> Overture to the Magic Flute<br /> Chamber<br /> Quintet in Eb for Horn and Strings<br /> Eine Kleine Nachtmusik<br /> Vocal<br /> Don Giovanni<br /> Idomeneo |
This is the script for ItemPopulate:
|
1 |
TreeViewItem tvi_current, tvi_child, tvi_root<br />long hdl_root<br />Integer ct<br />string categ[]<br /><br />// The current item is the parent for the new itemsThis.GetItem(handle, tvi_current)<br /><br />IF tvi_current.Level = 1 THEN<br /> // Populate level 2 with some standard categories<br /> categ[1] = "Orchestral"<br /> categ[2] = "Chamber"<br /> categ[3] = "Vocal"<br /><br /> tvi_child.StatePictureIndex = 0<br /> tvi_child.PictureIndex = 3<br /> tvi_child.SelectedPictureIndex = 4<br /> tvi_child.OverlayPictureIndex = 0<br /> tvi_child.Children = TRUE<br /><br /> FOR ct = 1 to UpperBound(categ)<br /> tvi_child.Label = categ[ct]<br /> This.InsertItemLast(handle, tvi_child)<br /> NEXT<br />END IF<br /><br />// Populate level 3 with music titles<br />IF tvi_current.Level = 2 THEN<br /><br /> // Get parent of current item - it's the root of<br /> // this branch and is part of the key for choosing<br /> // the children<br /><br /> hdl_root = This.FindItem(ParentTreeItem!, handle)<br /> This.GetItem(hdl_root, tvi_root) <br /><br /> FOR ct = 1 to 4<br /> // This statement constructs a label -<br /> // it is more realistic to look up data in a<br /> // table or database or accept user input <br /> This.InsertItemLast(handle, &<br /> tvi_root.Label + " Music " &<br /> + tvi_current.Label + String(ct), 3)<br /> NEXT<br />END IF |