准備工作:
1、在空白窗體上添加 TreeVIEw1 和 Button1
2、激活窗體的 OnCreate 事件, 激活 TreeVIEw 的 OnDblClick 事件
全部代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeVIEw;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure TreeVIEw1DblClick(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
arr1,arr2,arr3: array of TPoint;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
{准備測試用的動態數組數據}
SetLength(arr1, 2);
SetLength(arr2, 3);
SetLength(arr3, 4);
for i := 0 to Length(arr1)-1 do begin arr1[i].X := i+1; arr1[i].Y := i+4; end;
for i := 0 to Length(arr2)-1 do begin arr2[i].X := i+2; arr2[i].Y := i+5; end;
for i := 0 to Length(arr3)-1 do begin arr3[i].X := i+3; arr3[i].Y := i+6; end;
{添加到 TreeVIEw}
TreeVIEw1.Items.Clear;
TreeVIEw1.Items.AddChildObject(nil, 'Note1', arr1);
TreeVIEw1.Items.AddChildObject(nil, 'Note2', arr2);
TreeVIEw1.Items.AddChildObject(nil, 'Note3', arr3);
Button1.Caption := '重新裝載數據';
Button1.OnClick := OnCreate;
end;
procedure TForm1.TreeVIEw1DblClick(Sender: TObject);
var
node: TTreeNode;
arr: array of TPoint;
begin
{獲取當前節點}
node := TreeVIEw1.Selected;
if not Assigned(node.Data) then
begin
ShowMessageFmt('%s 中沒有攜帶其他數據', [node.Text]);
Exit;
end;
{獲取數組}
arr := node.Data;
{讀取數組的第一個數據}
ShowMessageFmt('%d, %d', [arr[0].X, arr[0].Y]);
{清空數組}
SetLength(arr, 0);
{清除數據指針}
node.Data := nil;
ShowMessageFmt('%s 中的數據已被刪除!', [node.Text]);
end;
end.