RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomTreeView.Selected Property

Specifies the selected node in the tree view.

Pascal
property Selected: TTreeNode;
C++
__property TTreeNode Selected;

Read Selected to access the selected node of the tree view. If there is no selected node, the value of Selected is nil (Delphi) or NULL (C++). 

Set Selected to set a node in the tree view. When a node becomes selected, the tree view's OnChanging and OnChange events occur. Also, if the specified node is the child of a collapsed parent item, the parent's list of child items is expanded to reveal the specified node. In this case, the tree view's OnExpanded and OnExpanding events occur as well. 

If the RightClickSelect property is true, the value of Selected is the last node that was clicked in the tree view, even if it was clicked with the right mouse button. This may differ from the node that is highlighted to indicate selection. 

If the MultiSelect property is true and the MultiSelectStyle property includes msControlSelect, then Selected returns the last node clicked on, even if that click deselected the node. For a current selection status when MultiSelect is true, refer to the Selections property.  

C++ Examples: 

 

/*
This example is an event handler for a popup menu item. The
handler enables editing of the current item in a TListView
control. The example would also work with a TTreeView
control, or with any instance of a control class derived
from TCustomViewControl.
*/
void __fastcall TForm1::EditItem1Click(TObject *Sender)
{
  TreeView1->Selected->EditText();
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TTreeNode *Node1;
  TreeView1->Items->Clear(); // remove any existing nodes
  // Add a root node
  TreeView1->Items->Add(NULL, "RootNode1");

  /* Set MyTreeNode to first node in tree view and add a child node to it */
  Node1 = TreeView1->Items->Item[0];
  TreeView1->Items->AddChild(Node1,"ChildNode1");

  // Add another root node
  TreeView1->Items->Add(Node1, "RootNode2");

  /* Reset Node1 to RootNode2 and add a child node to it */
  Node1 = TreeView1->Items->Item[2];

  TreeView1->Items->AddChild(Node1,"ChildNode2");

  /* Reset Node1 to ChildNode2 and add a child node to it */
  Node1 = TreeView1->Items->Item[3];
  TreeView1->Items->AddChild(Node1,"ChildNode2a");

   /* Add another child to ChildNode2 following ChildNode2a */
  TreeView1->Items->AddChild(Node1,"ChildNode2b");

  // add another root node
  TreeView1->Items->Add(TreeView1->Items->Item[0], "RootTreeNode3");
}

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
  PopupMenu1->Popup(Left + X, Top + Y);
}
/*
This example requires two buttons, three TEdits, a label and
a populated TreeView.  The following code defines a record
type of TMyRec and a record pointer type of PMyRec.
*/
typedef struct MyRec
{
  AnsiString FName, LName;
} TMyRec;

typedef TMyRec* PMyRec;
/*
Assuming these types are used, the following code adds a
node to TreeView1 as the last sibling of the node specified
by the Index.  A TMyRec record is associated with the added
item. The FName and LName fields are obtained from edit
boxes Edit1 and Edit2. The Index parameter is obtained from
edit box Edit3.  The item is added only if the Index is a
valid value.
*/

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int TreeViewIndex;
  TTreeNodes* items;

  // MyRecPtr will be added as an object to the treeview, so it's OK for MyRecPtr to be deleted in this proc.
  std::auto_ptr<TMyRec> MyRecPtr(new TMyRec);
  MyRecPtr->FName = Edit1->Text;
  MyRecPtr->LName = Edit2->Text;
  TreeViewIndex = StrToInt(Edit3->Text);
  items = TreeView1->Items;
  if (items->Count == 0)
    items->AddObject(NULL, "Item" + IntToStr(TreeViewIndex), MyRecPtr.get());
  else if ((TreeViewIndex < items->Count) && (TreeViewIndex >= 0))
    items->AddObject(
      items->Item[TreeViewIndex],
      "Item" + IntToStr(TreeViewIndex) + "after" + items->Item[TreeViewIndex]->Text,
      MyRecPtr.get());
}
/*
After an item containing a TMyRec record has been added, the
following code retrieves the FName and LName values
associated with the item and displays the values in a label.
*/
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  if (TreeView1->Selected->Data != NULL)
    Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + " " +
                 PMyRec(TreeView1->Selected->Data)->LName;
}

TTreeNode *currentNode = NULL;

void __fastcall TForm1::Button3Click(TObject *Sender)
{
//  PMyRec  MyRecPtr;
  TTreeNodes* items;

//  MyRecPtr = new TMyRec;
  // MyRecPtr will be added as an object to the treeview, so it's OK for MyRecPtr to be deleted in this proc.
  std::auto_ptr<TMyRec> MyRecPtr(new TMyRec);
  MyRecPtr->FName = Edit1->Text;
  MyRecPtr->LName = Edit2->Text;
  items = TreeView1->Items;
  if (currentNode != NULL)
    items->AddObject(currentNode, Edit4->Text + " after " + currentNode->Text, MyRecPtr.get());
}

void __fastcall TForm1::TreeView1MouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  THitTests HT;
  if (Sender->ClassNameIs("TTreeView"))
  {
    TTreeView *pTV = dynamic_cast<TTreeView *>(Sender);
    HT = pTV->GetHitTestInfoAt(X,Y);
    if (HT.Contains(htOnItem))
      currentNode = pTV->GetNodeAt(X,Y);
  }
}

void __fastcall TForm1::Button4Click(TObject *Sender)
{
  if (currentNode != NULL)
    Label2->Caption = IntToStr(currentNode->AbsoluteIndex);
}
/*
The following code uses GetNodeAt to add a dragged node as a
child of the node under the mouse when it is dropped.  This
example requires a populated TreeView.  Also, the TTreeView
DragMode property set to dmAutomatic, and the TTreeView
OnDragOver event handler must be implemented to accept the
drop.
*/
void __fastcall TForm1::TreeView1DragDrop(TObject *Sender, TObject *Source,
      int X, int Y)
{
  if (Source != dynamic_cast<TObject *>(TreeView1) || TreeView1->Selected == NULL)
    return;
  THitTests HT = TreeView1->GetHitTestInfoAt(X, Y);
  TNodeAttachMode AttachMode;
  TTreeNode *Item = TreeView1->GetNodeAt(X, Y);
  if (HT.Contains(htOnItem) || HT.Contains(htOnIcon))
    AttachMode = naAddChild;
  else if (HT.Contains(htNowhere))
    AttachMode = naAdd;
  else if (HT.Contains(htOnIndent))
    AttachMode = naInsert;
  else
    return;
  TreeView1->Selected->MoveTo(Item, AttachMode);
}

void __fastcall TForm1::TreeView1DragOver(TObject *Sender, TObject *Source,
      int X, int Y, TDragState State, bool &Accept)
{
  Accept = (Source == dynamic_cast<TObject *>(TreeView1));
}
/*
The following code uses Item to add the labels of all child
nodes of the selected node to a list box when a button is
clicked.  This example requires a button, a listbox, and a
populated treeview.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for (int i = 0; i < TreeView1->Selected->Count; i++)
    ListBox1->Items->Add(TreeView1->Selected->Item[i]->Text);
}

 

Delphi Examples: 

{
This example is an event handler for a popup menu item. The
handler enables editing of the current item in a TListView
control. The example would also work with a TTreeView
control, or with any instance of a control class derived
from TCustomViewControl.
}
procedure TForm1.EditItem1Click(Sender: TObject);
begin
  TreeView1.Selected.EditText;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  MyTreeNode1, MyTreeNode2: TTreeNode;
begin
  with TreeView1.Items do
  begin
    Clear; { remove any existing nodes }
    MyTreeNode1 := Add(nil, 'RootTreeNode1'); { Add a root node }
    { Add a child node to the node just added }
    AddChild(MyTreeNode1,'ChildNode1');

    {Add another root node}
    MyTreeNode2 := Add(MyTreeNode1, 'RootTreeNode2');
    {Give MyTreeNode2 to a child }
    AddChild(MyTreeNode2,'ChildNode2');

    {Change MyTreeNode2 to ChildNode2 }
    { and add a child node to it}
    MyTreeNode2 := TreeView1.Items[3];
    AddChild(MyTreeNode2,'ChildNode2a');

    {Add another child to ChildNode2, after ChildNode2a }
    AddChild(MyTreeNode2,'ChildNode2b');

    {add another root node}
    Add(MyTreeNode1, 'RootTreeNode3');
  end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  PopupMenu1.Popup(Left + X, Top + Y);
end;
{
This example requires three buttons, four TEdits, two labels
and a populated TreeView.  The following code defines a
record type of TMyRec and a record pointer type of PMyRec.
}
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    TreeView1: TTreeView;
    Label2: TLabel;
    Label3: TLabel;
    Button3: TButton;
    Edit4: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  type
  PMyRec = ^TMyRec;
  TMyRec = record
    FName: string;
    LName: string;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

{
Assuming these types are used, the following code adds a
node to TreeView1 as the last sibling of the selected node.
A TMyRec record is associated with the added item. The FName
and LName fields are obtained from edit boxes Edit1 and
Edit2.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  MyRecPtr: PMyRec;
  TreeViewIndex: LongInt;
begin
  New(MyRecPtr);
  MyRecPtr^.FName := Edit1.Text;
  MyRecPtr^.LName := Edit2.Text;
  with TreeView1 do
  begin
    TreeViewIndex := Selected.AbsoluteIndex;
    if Items.Count = 0 then
      Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
    else if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
      Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
  end;
end;
{
After an item containing a TMyRec record has been added, the
following code retrieves the FName and LName values
associated with the item and displays the values in a label.
} 
procedure TForm1.Button2Click(Sender: TObject);
begin
  if (TreeView1.Selected.Data <> nil) then { query only works on new nodes }
    Edit3.Text := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
                  PMyRec(TreeView1.Selected.Data)^.LName;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
  Edit4.Text := IntToStr(TreeView1.Selected.AbsoluteIndex);
end;
{
The following code uses GetNodeAt to add a dragged node as a
child of the node under the mouse when it is dropped.  This
example requires a populated TreeView.  Also, the TTreeView
DragMode property must be set to dmAutomatic, and the TTreeView
OnDragOver event handler must be implemented to accept the
drop.
}
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var 
  AnItem: TTreeNode;
  AttachMode: TNodeAttachMode;
  HT: THitTests;
begin
  if TreeView1.Selected = nil then Exit;
  HT := TreeView1.GetHitTestInfoAt(X, Y);
  AnItem := TreeView1.GetNodeAt(X, Y);
  if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent] <> HT) then 
  begin
    if (htOnItem in HT) or (htOnIcon in HT) then
      AttachMode := naAddChild
    else if htNowhere in HT then AttachMode := naAdd
    else if htOnIndent in HT then AttachMode := naInsert;
    TreeView1.Selected.MoveTo(AnItem, AttachMode); 
  end;
end;

procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TTreeView;
end;
{
The following code uses Item to add the labels of all child
nodes of the selected node to a list box when a button is
clicked.  This example requires a button, a listbox, and a
populated treeview.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
begin
  for I := 0 to (TreeView1.Selected.Count - 1) do
    ListBox1.Items.Add(TreeView1.Selected.Item[I].Text);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!