Ver Mensaje Individual
  #9  
Antiguo 19-10-2022
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is offline
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 18.293
Reputación: 10
Neftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en brutoNeftali [Germán.Estévez] Es un diamante en bruto
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 [-]
// 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           lbl       := TLabel.Create(Self);           lbl.Name  := 'lbl'+IntToStr(contcomp);           lbl.Parent:= Cat;           lbl.Caption:= Trim(DM.QryPButtonsDatosDetalle.Value);           lbl.Top   := tp;           lbl.Left  := ll;          // Crear el control segun su tipo y ubicarlo al lado del label         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:

// 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));       // Crear el componente (usando TWinControl) a partir de la clase
    comp.Name  := comp.ClassName + IntToStr(contcomp);
    comp.Parent:= Cat;
    comp.Tag   := iTag;
    comp.Width := iWidth;
    // comp.Text  := '';
    comp.Top   := tp;
    comp.Left  := iLeft;
    if IsPublishedProp(comp, 'Text') then                   // RTTI para propiedades published
      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.
__________________
Germán Estévez => Web/Blog
Guía de estilo, Guía alternativa
Utiliza TAG's en tus mensajes.
Contactar con el Clubdelphi

P.D: Más tiempo dedicado a la pregunta=Mejores respuestas.
Responder Con Cita