Difference between revisions of "TTreeView"
m |
|||
Line 8: | Line 8: | ||
To add the expansion symbol to a node without subitems use: | To add the expansion symbol to a node without subitems use: | ||
− | + | <delphi>MyNode.HasChildren := True;</delphi> | |
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. | 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. | ||
Line 23: | Line 23: | ||
Code: | Code: | ||
− | < | + | <delphi>procedure TForm1.Button1Click(Sender: TObject); |
− | + | var | |
− | + | i: integer; | |
− | + | s: string; | |
− | + | begin | |
− | //if there is no nodes, create a root node with a parent of Nil | + | // if there is no nodes, create a root node with a parent of Nil |
if TreeView1.Items.Count = 0 then | if TreeView1.Items.Count = 0 then | ||
begin | begin | ||
Line 35: | Line 35: | ||
end; | end; | ||
− | //Set up a simple text for each new node - Node1 , Node2 etc | + | // Set up a simple text for each new node - Node1 , Node2 etc |
− | i:=treeview1.Items.Count; | + | i := treeview1.Items.Count; |
− | s:= 'Node '+inttostr(i); | + | s := 'Node ' + inttostr(i); |
//Add a new node to the currently selected node | //Add a new node to the currently selected node | ||
if TreeView1.Selected <> nil then | if TreeView1.Selected <> nil then | ||
Treeview1.Items.AddChild(Treeview1.Selected ,s); | Treeview1.Items.AddChild(Treeview1.Selected ,s); | ||
− | + | end; | |
− | + | procedure TForm1.Button2Click(Sender: TObject); | |
//A procedure to recursively delete nodes | //A procedure to recursively delete nodes | ||
Line 52: | Line 52: | ||
end; | end; | ||
− | + | begin | |
if TreeView1.Selected = nil then exit; | if TreeView1.Selected = nil then exit; | ||
//If selected node has child nodes, first ask for confirmation | //If selected node has child nodes, first ask for confirmation | ||
Line 59: | Line 59: | ||
[mbYes,mbNo],0) <> mrYes then exit; | [mbYes,mbNo],0) <> mrYes then exit; | ||
DeleteNode(TreeView1.Selected); | DeleteNode(TreeView1.Selected); | ||
− | + | end;</delphi> | |
− | </ | ||
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" | 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" | ||
Line 71: | Line 70: | ||
# Set to DmAutomatic, the property "Drag Mode" of your treeview | # Set to DmAutomatic, the property "Drag Mode" of your treeview | ||
# Create an event for "OndragOver": | # Create an event for "OndragOver": | ||
− | < | + | <delphi>procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); |
− | procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); | ||
begin | begin | ||
Accept := true; | Accept := true; | ||
− | end; | + | end; |
− | + | </delphi> | |
− | </ | ||
hope this helps | hope this helps | ||
+ | |||
+ | [[Category:Components]] |
Revision as of 12:53, 4 September 2010
Adding a new item in code
Use TTreeView.Items.AddChild or AddChildObject.
Creating a TreeView which loads items only when expansing
To add the expansion symbol to a node without subitems use:
<delphi>MyNode.HasChildren := True;</delphi>
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:
<delphi>procedure TForm1.Button1Click(Sender: TObject); var
i: integer; s: string;
begin
// if there is 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;</delphi>
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.
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":
<delphi>procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin
Accept := true;
end; </delphi>
hope this helps