Items in a tree control are managed as a linear list or array of items. All methods and properties 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 0||First Item|
|Index 1||Second Item|
|Index 2||Third Item|
|Index 3||Fourth Item|
Once a new item is added, the tree control looks like this:
|Index 0||New Item|
|Index 1||First Item|
|Index 2||Second Item|
|Index 3||Third Item|
|Index 4||Fourth 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 the Item.ID property. 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 the Items.ItemIndex property.
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 the Item.ID property. Later, regardless of how many other items have been added or removed, using Items.ItemIndex with the saved item ID will return the current item index.