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:
int ct long h_item = 0 treeviewitem tvi FOR ct = 1 TO UpperBound(item_label) tvi.Label = item_label[ct] tvi.Data = item_data[ct] tvi.PictureIndex = 1 tvi.SelectedPictureIndex = 2 tvi.Children = TRUE tvi.StatePictureIndex = 0 tvi.DropHighlighted = TRUE h_item = tv_1.InsertItemSort(0, tvi) NEXT
After inserting all the items, this code scrolls the TreeView back to the top and makes the first item current:
// Scroll back to top h_item = tv_1.FindItem(RootTreeItem!, 0) tv_1.SetFirstVisible(h_item) tv_1.SelectItem(h_item)