例子如下:
class TDynDataForm=TForm
Table1:TTable;
private
CtrlArry:TStrings;
protected
procedure BuildForm;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
publish
end;
implement
constructor TDynDataForm.Create(AOwner:TComponent);
begin
inherited;
CtrlArry:=TStringList.Create;
end;
destructor TDynDataForm.Destroy(AOwner:TComponent);
begin
while CtrlArry.count>0 do begin
CtrlArry.Objects[CtrlArry.count-1].free;
CtrlArry.delete(CtrlArry.count-1);
end;
inherited;
end;
procedure TDynDataForm.BuildForm;
var i:integer;
tmpctrl:TWinControl;
begin
Table1.open;
//if you do want this order
//you can use a TList to change the order
for i:=0 to Table1.FieldsCount-1 do begin
case Table1.Fields.DataType of
ftstring:begin
tmpctrl:=TDBEdit.Create;
//do something that you want
//and assign tmpctrl property
end;
ftMemo: begin
tmpctrl:=TDBMemo.Create;
//do something that you want
//and assign tmpctrl property
end;
end;
//add you case
end;
CtrlArry.AddObject(Table1.Fields.FieldName,tmpCtrl);
end;
end;