Hide

SftTree/DLL 7.5 - Tree Control

Display
Print

Item IDs

Items in a tree control are managed as a linear list or array of items. All API functions that access a particular item require the item index. This is a zero-based number describing the item position. The first item in the tree control has item index 0, the second is item 1, etc. Consequently, if an item is inserted at the top of the list, all previously added items now receive a new item index.

Index 0First Item
Index 1Second Item
Index 2Third Item
Index 3Fourth Item

Once a new item is added, the tree control looks like this:

Index 0New Item
Index 1First Item
Index 2Second Item
Index 3Third Item
Index 4Fourth Item

An item index describes the position in the tree control, so it is subject to change if items are added or removed.

In some cases it may be easier to work with a constant value describing an item no matter where it is located in the list of items. This is possible using item IDs. Once an item has been added to the tree control, its ID can be retrieved using GetItemID. The returned item ID does not change, even if other items are added or removed. Given an item ID, the current item index can be found using GetItemIndex.

Saving the item ID is most useful for top level parent items. Top level items usually show item categories. Since adding or removing child items of other parent items changes a top level parent's index, it is easier to work with item IDs. When a top level parent item is added, save the item ID retrieved using GetItemID. Later, regardless of how many other items have been added or removed, a call to GetItemIndex with the saved item ID will return the current item index.

SFTTREE_ID savedID; 
int index; 
index = m_Tree1.AddString("Category 1"); 
savedID = m_Tree1.GetItemID(index); 
. . . other items added/inserted and removed
// retrieve the item index for "Category 1" 
index = m_Tree1.GetItemIndex(savedID);
. . . processing for Category 1