Difference between revisions of "TTreeView"
Line 12: | Line 12: | ||
To add the first node, it becomes | To add the first node, it becomes | ||
− | TreeView1.Items.Add(nil, ' | + | TreeView1.Items.Add(nil, 'First node'); |
Use TTreeView.Items.AddChild or AddChildObject to add child node. | Use TTreeView.Items.AddChild or AddChildObject to add child node. | ||
− | TreeView1.Items.AddChild(ATreeNode, ' | + | TreeView1.Items.AddChild(ATreeNode, 'Child node'); |
==Creating a TreeView which loads items only when expansing== | ==Creating a TreeView which loads items only when expansing== |
Revision as of 10:36, 25 December 2020
│
English (en) │
español (es) │
suomi (fi) │
français (fr) │
magyar (hu) │
русский (ru) │
A TTreeView is a graphical control element that presents a hierarchical view of information. Each item can have a number of subitems.
An item can be expanded to reveal subitems, if any exist, and collapsed to hide subitems.
Adding a new item in code
To add a sibling node to pre-existing node, use TTreeView.ITems.Add.
TreeView1.Items.Add(ATreeNode, 'Added node');
To add the first node, it becomes
TreeView1.Items.Add(nil, 'First node');
Use TTreeView.Items.AddChild or AddChildObject to add child node.
TreeView1.Items.AddChild(ATreeNode, 'Child node');
Creating a TreeView which loads items only when expansing
To add the expansion symbol to a node without subitems use:
MyNode.HasChildren := True;
And then set an event handler for the OnExpanding event. In this even you should return if the expansion can actually be made or not and when yes, you should add subitems to the node. If the expansion cannot be done, the expansion symbol will be automatically removed even if you have previously set HasChildren to true.
A Short example of using TTreeview
Here is a quick & dirty example - I tested on Windows Lazarus 0.9.26:
Create a new application. On Form1 add an empty treeview, a button1 with caption "Add Child" and a button2 with caption "Delete"
For the buttons' OnClick events, assign the following code, compile & run.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
s: string;
begin
// if there are no nodes, create a root node with a parent of nil
if TreeView1.Items.Count = 0 then
begin
Treeview1.Items.Add (nil,'Root Node');
exit;
end;
// Set up a simple text for each new node - Node1 , Node2 etc
i := treeview1.Items.Count;
s := 'Node ' + inttostr(i);
//Add a new node to the currently selected node
if TreeView1.Selected <> nil then
Treeview1.Items.AddChild(Treeview1.Selected ,s);
end;
procedure TForm1.Button2Click(Sender: TObject);
//A procedure to recursively delete nodes
procedure DeleteNode(Node:TTreeNode);
begin
while Node.HasChildren do
DeleteNode(node.GetLastChild);
TreeView1.Items.Delete(Node) ;
end;
begin
if TreeView1.Selected = nil then
exit;
//if selected node has child nodes, first ask for confirmation
if treeview1.Selected.HasChildren then
if messagedlg( 'Delete node and all children ?',mtConfirmation, [mbYes,mbNo],0 ) <> mrYes then
exit;
DeleteNode(TreeView1.Selected);
end;
When running, the treeview is empty. If you click "Add Child", a root node is created. After that a child will be added to any selected node by clicking "Add Child"
Delete will delete the currently selected node. If it doesn't have children, it will delete it immediately, but if it has children, it will first ask.
Example of using Multiple Node Selection for Multiple User Selections
If you select your TTreeview component, go to the object inspector and in the options sections set 'tvoAllowMultiSelect' to true. Then, to iterate over the selected nodes and obtain the paths of the chosen folders or files, the following example will populate the text paths of those selected nodes to a memo field :
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
if TreeView1.SelectionCount=0 then
exit;
Memo1.Lines.Clear;
for i:=0 to TreeView1.SelectionCount-1 do
Memo1.Lines.Add(TreeView1.Selections[i].GetTextPath);
end;
Freeing TreeNode Data
Use the TreeView's OnDeletion event to free your object.
procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
TMyObject(Node.Data).Free;
Node.Data := nil;
end;
Using Drag and Drop in a TreeView
If you want to allow a drag and drop function in your treeview, you need to :
- Set to DmAutomatic, the property "Drag Mode" of your treeview
- Create an event for "OndragOver":
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := true;
end;
Comments on Events
Event hanlders are convenient places for developing VCL applications. There are some peculiar events for TTreeview.
When selected node is changed
OnChange, OnChanging - These events are called when selected node is shifted from one to another. OnChanging occur before the shift is done, and OnChange after actual shift. The headers of procedures are as follows. To prevent the shift, one may set AllowChange variable of OnChanging event handler to false.
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode); procedure TForm1.TreeView1Changing(Sender: TObject; Node: TTreeNode; var AllowChange: Boolean);
Caution must be taken, as node variable of both procedures point to "new" node, which is to move to. In Delphi, node variable of OnChanging event handler points to the node that selection is about to leave.
When a node is inserted
When a new node is inserted, related events occur in following order.
1) OnNodeChanged 2) (OnChanging): This is called if there was a previously selected node. 3) OnSelectionChanged 4) OnChange
When deleting a node
When the selected node is deleted, following events occur.
1) OnDelete 2) OnChanging 3) SelectionChanged 4) OnChange : This is called because when you delete a node, another node is selected.
If unselected node is deleted, e.g. from program, then only OnDelete event handler is called.
When Form is Closed
OnChanging event occurs, as the selected node is "de"selected. But OnChanging eventhander is executed AFTER the form is closed. This means that you cannot access the form's other data (other controls, other variables, etc.) within OnChanging event handler procedure.
When Drag&Drop is done
When you drag a node to another node and drop, following events occur.
1) OnSelectionChanged 2) OnNodeChanged 3) OnChange
See also