随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


问题来源: http://www.cnblogs.com/del/archive/2009/12/18/1627110.html#1799657

准备工作:
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]);
  {清除数据指针}
  node.Data := nil;
  ShowMessageFmt('%s 中的数据已被删除!', [node.Text]);
end;

end.

posted on 2010-04-12 20:59  万一  阅读(...)  评论(...编辑  收藏