Yo creo que se podría "arreglar" la parte de los IF que crea los componentes. Es muy repetitivo y usando RTTI y la herencia de clases podría quedarse más sencillo.
Y si el número de controles diferentes crece, te saldrá más a cuente para no tener qui ir copiando y pegando ese trozo:
Así esto:
Código Delphi
[-] for j := 0 to (DM.QryPButtons.RecordCount -1) do
begin
contcomp := contcomp + 1;
lbl := TLabel.Create(Self);
lbl.Name := 'lbl'+IntToStr(contcomp);
lbl.Parent:= Cat;
lbl.Caption:= Trim(DM.QryPButtonsDatosDetalle.Value);
lbl.Top := tp;
lbl.Left := ll;
if (Trim(DM.QryPButtonsDatosTipo.Value) = 'TEdit') Or (Trim(DM.QryPButtonsDatosTipo.Value) = 'Edit') then
begin
edt := TEdit.Create(Self);
edt.Name := 'Edt'+IntToStr(contcomp);
edt.Parent:= Cat;
edt.Tag := DM.QryPButtonsidDatos.Value;
edt.Width := ew;
edt.Text := '';
edt.Top := tp;
edt.Left := cl;
tp := tp + eh + sp;
end;
if (Trim(DM.QryPButtonsDatosTipo.Value) = 'TMemo') Or (Trim(DM.QryPButtonsDatosTipo.Value) = 'Memo') then
begin
mem := TMemo.Create(Self);
mem.Name := 'Mem'+IntToStr(contcomp);
mem.Parent:= Cat;
mem.Tag := DM.QryPButtonsidDatos.Value;
mem.Width := mw;
mem.Text := '';
mem.Top := tp;
mem.Left := cl;
tp := tp + mh + sp;
end;
if (Trim(DM.QryPButtonsDatosTipo.Value) = 'TCheckbox') Or (Trim(DM.QryPButtonsDatosTipo.Value) = 'Checkbox') then
begin
chk := TCheckBox.Create(Self);
chk.Name := 'Chk'+IntToStr(contcomp);
chk.Parent:= Cat;
chk.Tag := DM.QryPButtonsidDatos.Value;
chk.Width := cw;
chk.Caption:='';
chk.Top := tp;
chk.Left := cl;
tp := tp + ch + sp;
end;
Se podría quedar en algo como esto:
Código Delphi
[-] for j := 0 to (DM.QryPButtons.RecordCount -1) do
begin
contcomp := contcomp + 1;
CreateComponent(TLabel,DM.QryPButtonsidDatos.Value, 5, ll, DM.QryPButtons.FieldByName('CaptionLabel').AsString);
var sDatosTipo:string := Trim(DM.QryPButtons.FieldByName('DatosTipo').AsString);
if (sDatosTipo = 'TEdit') Or (sDatosTipo = 'Edit') then begin
CreateComponent(TEdit,DM.QryPButtonsidDatos.Value, ew, cl, '');
tp := tp + eh + sp;
end;
if (sDatosTipo = 'TMemo') Or (sDatosTipo = 'Memo') then begin
CreateComponent(Tmemo,DM.QryPButtonsidDatos.Value, mw, cl, '');
tp := tp + mh + sp;
end;
if (sDatosTipo = 'TCheckbox') Or (sDatosTipo = 'Checkbox') then begin
CreateComponent(TCheckbox, DM.QryPButtonsidDatos.Value, cw, cl, '');
tp := tp + ch + sp;
end;
QryButtons.Next;
end;
Y con el procedimiento definido así:
Código Delphi
[-]
procedure CreateComponent(CompClass:TComponentClass; iTag:integer; iWidth, iLeft:integer; sCaption:string);
begin
comp := TWinControl(CompClass.Create(Self));
comp.Name := comp.ClassName + IntToStr(contcomp);
comp.Parent:= Cat;
comp.Tag := iTag;
comp.Width := iWidth;
comp.Top := tp;
comp.Left := iLeft;
if IsPublishedProp(comp, 'Text') then
SetPropValue(comp, 'Text', String.Empty);
if IsPublishedProp(comp, 'Caption') then
SetPropValue(comp, 'Caption', sCaption);
end;
Y hay que añadir
System.TypInfo al
USES.
la idea con esto es utilizar
RTTI para poder crear los diferentes componentes (diferentes clases) en un mismo punto. Herencia para las diferentes clases (
TComponentClass) y
RTTI para preguntar y asigar las propiedades
published de los componentes.