提供一个标准的函数: //============================================================================== //动态创建控件****************************************************************** //============================================================================== function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl; begin if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then begin Result := nil; exit; end; Result := Owner.FindComponent(CompName) as TControl; if Result=nil then begin Result := CompType.Create(Owner); with Result do begin if Owner is TwinControl then begin SetBounds(Left,Top,Width,Height); Parent := TwinControl(Owner);{如果是可视构件,则显示之} if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点} end; end; Result.Name := CompName; end else {Result<>Nil} if not(Result is CompType) then begin Result := nil; Exit; end; Result.Visible := True; end; { 对于未知数量的控件组,利用TList var ControlList: Tlist; CreateNum: integer; const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。 var i:integer; APoint: Pointer; ControlList := TList.Create; ControlList.Clear; CreateNum := 10; for i:=1 to CreateNum do begin APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建 ControlList.Add(APoint); end; TButton(ControlList.Items[i]).Caption := 'XXXX';}
try
Button1.Parent := Self;
...
finally
Button1.Free;
end;
L: TQRLabel;
......
L:=TQRLabel.Create(ColumnHeaderBand1);
L.Parent:=ColumnHeaderBand1;
L.Caption:='序号';
L.Top:=iTop;
L.Left:=iLeft;
L.Width:=40;
L.AlignToBand:=false;
L.Alignment:=taCenter;
button1.parent := form1;
//==============================================================================
//动态创建控件******************************************************************
//==============================================================================
function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl;
begin
if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then
begin
Result := nil;
exit;
end;
Result := Owner.FindComponent(CompName) as TControl;
if Result=nil then
begin
Result := CompType.Create(Owner);
with Result do
begin
if Owner is TwinControl then
begin
SetBounds(Left,Top,Width,Height);
Parent := TwinControl(Owner);{如果是可视构件,则显示之}
if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点}
end;
end;
Result.Name := CompName;
end
else {Result<>Nil}
if not(Result is CompType) then
begin
Result := nil;
Exit;
end;
Result.Visible := True;
end;
{ 对于未知数量的控件组,利用TList
var ControlList: Tlist; CreateNum: integer;
const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。
var i:integer; APoint: Pointer;
ControlList := TList.Create;
ControlList.Clear;
CreateNum := 10;
for i:=1 to CreateNum do
begin
APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建
ControlList.Add(APoint);
end;
TButton(ControlList.Items[i]).Caption := 'XXXX';}