unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var p:^Integer;
i:Integer;
begin
For i:=0 to 10 do
begin
New(p);
p^:=i;
TreeView1.Items.AddObject(nil,'New Node',p);
//把要加入到节点的数据作为指针加入到Node的Data中;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
Var i:Integer;
P:^Integer;
begin
For i:=0 to TreeView1.Items.Count-1 do
begin
P:=TreeView1.Items.Data;
TreeView1.Items.Data:=nil;
Dispose(p);
//有分配就有释放。
end;
end;
procedure TForm1.TreeView1DblClick(Sender: TObject);
Var P:^Integer;
begin
if TreeView1.Selected<>nil then
begin
P:=TreeView1.Selected.Data;
ShowMessage(IntToStr(P^));
//从Node.Data中取出数据。
end;
end;
end.