PDA

View Full Version : Samples/Examples on Treeview


kate
02-01-2003, 08:29 AM
Hi,
Has anyone comes across good sample that demonstrate how to use the treeview. Required detailed demos that assist the user to construct the treeview basic up.

Thanks.

krukmat
02-01-2003, 01:31 PM
This example create 3 root nodes. In the first one has 1 child. The second one has 2 childs. One of these childs has a child. Finally the third root has no childs.
The code was extracted from Delphi Help
(go to TTreeView -> Items -> TTreeNodes -> Add)
Prove it. I think could help you.
Don't forget to click Accept if it helped you.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(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 }
Add(MyTreeNode2,'ChildNode2b');

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

end;

end.

krukmat
02-01-2003, 01:43 PM
Sorry here it is a more complete example:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//Just for initialization
procedure TForm1.Button1Click(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 }
Add(MyTreeNode2,'ChildNode2b');

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

end;


//To delete the selected node...
procedure TForm1.Button2Click(Sender: TObject);
begin
TreeView1.Selected.Delete; //Delete the selected node
end;

//to insert the selected node
procedure TForm1.Button3Click(Sender: TObject);
begin
TreeView1.items.Insert(TreeView1.Selected,'Another Child'); //Insert a child in the level of the selected node ...
end;
//if you want to edit the text of the selected node just make a click on the text, edit it and then make another click. Easy !
end.