我有六个声明了的TButtonButton1 。Button6
在一个方法中声明
const
buttonArray : Array[0..5] of TObject = (Button1,Button2,Button3,Button4,Button5,Button6);报错,const改称var也报错该怎么写啊
在一个方法中声明
const
buttonArray : Array[0..5] of TObject = (Button1,Button2,Button3,Button4,Button5,Button6);报错,const改称var也报错该怎么写啊
在form中放置6个button按钮,然后再声明控件数组变量,然后对控件数组变量赋值。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
btnarray:array[1..6] of TButton;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
btnarray[1]:=Button1;
btnarray[2]:=Button2;
btnarray[3]:=Button3;
btnarray[4]:=Button4;
btnarray[5]:=Button5;
btnarray[6]:=Button6;
end;end.
能有什么简便方法对button数组赋值么,假如我有100个button
begin
buttonArray[i] := TButton.Create(AOwner);
...
end;不知道行不行,你试试啊。
var
i: Integer;
aComponent: TComponent;
begin
for i:= Low(btnarray) to High(btnarray) do
begin
aComponent := AParent.FindComponent('Button' + IntToStr(i));
if (aComponent <> nil) and (aComponent is TButton) then
btnarray[i] := TButton(aComponent)
else
btnarray[i] := nil;
end;
end;
if Components[i].ClassType = TButton then
TButton(Components[i]).Caption= 'Button'+IntToStr(i);
如果Button所在的容器上有多种控件,可以甄别赋值、调用。