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:
Se podría quedar en algo como esto:
// Para cada Grupo, crear los botones...
for j := 0 to (DM.QryPButtons.RecordCount -1) do
begin
contcomp := contcomp + 1;
//Crear el label con la nota del campo a introducir dato
CreateComponent(TLabel,DM.QryPButtonsidDatos.Value, 5, ll, DM.QryPButtons.FieldByName('CaptionLabel').AsString);
var sDatosTipo:string := Trim(DM.QryPButtons.FieldByName('DatosTipo').AsString);
// Crear el control segun su tipo y ubicarlo al lado del label
if (sDatosTipo = 'TEdit') Or (sDatosTipo = 'Edit') then begin
// Crear un Edit
CreateComponent(TEdit,DM.QryPButtonsidDatos.Value, ew, cl, '');
tp := tp + eh + sp;
end;
if (sDatosTipo = 'TMemo') Or (sDatosTipo = 'Memo') then begin
// Crear un memo
CreateComponent(Tmemo,DM.QryPButtonsidDatos.Value, mw, cl, '');
tp := tp + mh + sp;
end;
if (sDatosTipo = 'TCheckbox') Or (sDatosTipo = 'Checkbox') then begin
// crear un checkbox
CreateComponent(TCheckbox, DM.QryPButtonsidDatos.Value, cw, cl, '');
tp := tp + ch + sp;
end;
//but := TButtonItem.Create(Cat.Categories.Add.Items);
//but.Caption := QryButtons.FieldByName('DatosDetalle').AsString;
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 ls 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.