注册 登录
编程论坛 Delphi论坛

求助:控件数组问题

Domes 发布于 2008-02-09 09:26, 1470 次点击
在vb6中,可以设计控件数组(可视化的控件,如button),在编程实际中很有用,请问如何在delphi2005中实现呢?
如:ommand1[i].text=“”(vb6)
2 回复
#2
anthony6342008-02-20 21:11
你说的是不是delphi的数组属性啊?
type
 TArrButton = array of TButton;

  TTestProperty = class(TObject)
  private
    fButton: TArrButton;
    function getaButton(i: Integer): TButton;
  public
    constructor Create(alength: Integer);
    destructor destroy(); override;
    property MyButton[index: Integer]: TButton read getaButton;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    fTestProperty: TTestProperty;
  public
    { Public declarations }
  end;



var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TTestProperty }

constructor TTestProperty.Create(alength: Integer);
var
  I: Integer;
begin
  SetLength(fButton, 10);
  for i := Low(fButton) to High(fButton) do
  begin
    fButton[i] := TButton.Create(nil);
    fButton[i].SetBounds(50*i, 50*i, 10*i, 10*i);
    fButton[i].Parent := Form1;
    fButton[i].Name := TButton.ClassName;
    fButton[i].Caption := IntToStr(i) + ' 什么啊 ';
  end;
end;

destructor TTestProperty.destroy;
var
  I: Integer;
begin
  for i := Low(fButton) to High(fButton) do
  begin
    FreeAndNil(fButton[i]);
  end;
  SetLength(fButton, 0);
  inherited destroy();
end;

function TTestProperty.getaButton(i: Integer): TButton;
begin
  if (i >= Low(fButton)) and (i <= High(fButton)) then
  begin
     Result := fButton[i];
  end
  else
  begin
    Result := nil;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  tempStr: string;
  index: Integer;
begin
  index := StrToIntDef(Edit1.Text, 0);
  tempStr := fTestProperty.MyButton[index].Caption;
  TButton(Sender).Caption := tempStr;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fTestProperty := TTestProperty.Create(10);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  fTestProperty.Free();
end;

end.
#3
jgio2008-02-26 09:33
www. 慧都控件网,国内最大的专业控件代理商与技术支持商。
tel:023-66090381    QQ:903506412
1