Overview
In a basic tree list, the nodes are just text. It is also possible to have the nodes associated with objects or records (ie, they can store a pointer).
Each node has 2 index numbers - one absolute and one relative to its parent node. When new nodes are added, or existing nodes are deleted, the indices of the other nodes change accordingly.
Component | Property | Comments |
---|---|---|
TTreeView | Items | TTreeNodes object - main access to nodes
cnt := TreeView1.Items.Count; SomeNode := TreeView1.Items[5]; // Zero based access // 0 to Count-1 |
Selected | Use this to access or set the currently selected node.
If the RightClickSelect property is True, this may differ from the node that is highlighted to indicate selection. | |
RightClickSelect | When True, it causes the Selected property to change when a node is right clicked - default=False | |
HideSelection | When True, the highlight is hidden when focus move to another control; when False, the node always appears selected - default=True | |
ShowButtons | Controls display of the +/- buttons - default=True | |
ShowLines | Controls display of lines that link child nodes to their corresponding parent nodes - default=True | |
Indent | Specifies the number of pixels child nodes are indented - default=19 | |
TTreeNodes | Count | The number of nodes |
Item | Used to access to a node by its position. Zero is the first node.
FirstNode := TreeView1.Items.Item[0];can be written FirstNode := TreeView1.Items[0];Very slow to use. | |
TTreeNode | Text | What is displayed |
Level | Level of indentation of this node within the tree view control - zero is the top | |
Data | Points to an object or record
PMyRec(TreeView1.Selected.Data)^.FName | |
Selected | Boolean - True for the currently selected node | |
Focused | Controls the node's appearance - I can't make setting this do anything |
Component | Method | Comments |
---|---|---|
TTreeView | OnCompare | Use this to provide a non-alphabetic sort order. |
OnEditing | Called before the user edits a node's Text property. Set AllowEdit to False to prevent the user from editing the specified node. | |
OnEdited | Called after the user edits a node's Text property. Use this to make the name change permanent - update filename, table field value, etc. | |
GetNodeAt(x, y)
GetHitTestInfoAt(x, y) | Used with mouse position for drag and drop (see example in the help) | |
LoadFromFile, LoadFromStream, SaveToFile, SaveToStream | The data format is not specified. My experiments indicate that only the node text is saved, Tabs are used for level indications. (Not very useful.) | |
OnClick, OnDblClick, OnContextPopup, ... | For each of these, there is only one event for the entire tree list,
use Selected to get the node.
procedure TForm1.TreeView1DblClick(Sender: TObject); begin Edit1.Text := TreeView1.Selected.Text; end; | |
TTreeNodes | AddChildFirst, AddChild, AddChildObjectFirst, AddChildObject, AddFirst, Add, AddObjectFirst, AddObject, Insert | Used to add nodes
I see no difference between Add and AddChild. |
BeginUpdate EndUpdate | Use BeginUpdate to prevent screen repaints and to speed up your code while adding nodes to the tree view. Use EndUpdate to update the screen. | |
TTreeNode | EditText | Allows the user to modify the displayed text.
OnEditing is called before the node is placed in edit mode.
OnEdited is called if the value is changed.
(Esc cancels the change.)
TreeView1.Items [3].Selected := true; TreeView1.Items [3].EditText; |
GetNext | Returns the next node including nodes that aren't visible and child nodes. | |
GetNextSibling | Returns the next node in the tree view at the same level as the calling node, regardless of whether it's visible. | |
GetNextVisible | Returns the next visible node in the tree view after the calling node. | |
GetFirstChild, GetNextChild, GetPrevChild | Locate a node in the list of immediate children of the tree view node. |
Examples in the Delphi Help
Images
S I Node text | | | ----- Normal/Selected image (changes automatically) ------- State imageBased on trial and error (there is no help) the images should be 16x16 *.bmp files - the size of the *.bmp files in
C:\Program Files\Common Files\Borland Shared\Images\Icons\
When ImageIndex and SelectedIndex contain different values, the image is automatically changed when you click on (select) that node. (For example, consider the open and closed directory folders in Windows Explorer.)
The State image is intended to show both states of a check box, radio button, and similar state indicators ... however, you may use it for anything you like.
Property | Usage |
---|---|
TCustomTreeView.Images | This should point to an ImageList |
TTreeNode.ImageIndex | This selects an image from TCustomTreeView.Images |
TTreeNode.SelectedIndex | Specifies the image from TCustomTreeView.Images to display when the tree node is selected |
TTreeNode.OverlayIndex | Specifies the image from TCustomTreeView.Images to draw transparently over the current image, for example. an X to indicate "no longer available" |
TCustomTreeView.StateImages | This should point to an ImageList |
TTreeNode.StateIndex | This selects an image from TCustomTreeView.StateImages |
TCustomTreeView.OnGetImageIndex | These can be used to change the image index for a node before it is drawn ... however, since they are called for every node, you must test to determine which node is being passed. If present, keep these simple. |
TCustomTreeView.OnGetSelectedIndex |
Specific images are selected via the indices
Tab Component Win32 TImageList Win32 TTreeView
Component | Property | Value | Comments |
---|---|---|---|
TreeView1 | Images | ImageList1 | |
StateImages | ImageList1 |
C:\Program Files\Common Files\Borland Shared\Images\Icons\