RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TTreeView.Items Property

Lists the individual nodes that appear in the tree view control.

Pascal
property Items: TTreeNodes;
C++
__property TTreeNodes Items;

Individual nodes in a tree view are TTreeNode objects. These individual nodes can be accessed by using the Items property along with the item's index into the tree view. For example, to access the second item in the tree view, you could use the following code.

MyTreeNode := TreeView1.Items[1];

 

MyTreeNode = TreeView1->Items[1];

When setting this property at design-time in the Object Inspector the Tree View Items Editor appears. Use the New Item and New SubItem buttons to add items to the tree view. Use the Text property to modify what text is displayed in the label of the item. 

At run-time nodes can be added and inserted by using the TTreeNodes methods AddChildFirst, AddChild, AddChildObjectFirst, AddChildObject, AddFirst, Add, AddObjectFirst, AddObject, and Insert.

Note: Accessing tree view items by index can be time-intensive, particularly when the tree view contains many items. For optimal performance, try to design your application so that it has as few dependencies on the tree view's item index as possible.
 

C++ Examples: 

 

/*
The following example deletes an item in the tree view when
the user clicks on it, and if the checkbox is checked. This example
requires a populated TreeView.
*/
void __fastcall TForm1::TVMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  THitTests HT;
  if (CheckBox1->Checked && Sender->ClassNameIs("TTreeView"))
  {
    TTreeView *pTV = (TTreeView *)Sender;
    HT = pTV->GetHitTestInfoAt(X,Y);
    if (HT.Contains(htOnItem))
      pTV->Items->Delete(pTV->GetNodeAt(X,Y));
  }
}

 

Delphi Examples: 

{
The following example deletes an item in the tree view when
the user clicks on it, and if the checkbox is checked. This example
requires a populated TreeView.
} 
procedure TForm1.TVMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  HT : THitTests;
begin
if (CheckBox1.Checked) and (Sender is TTreeView) then
  begin
  with Sender as TTreeView do 
    begin
    HT := GetHitTestInfoAt(X,Y);
    if (htOnItem in HT) then
      Items.Delete(GetNodeAt(X,Y));
    end;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!