Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Coloboración Paypal con ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 19-10-2022
Avatar de Neftali [Germán.Estévez]
Neftali [Germán.Estévez] Neftali [Germán.Estévez] is online now
[becario]
 
Registrado: jul 2004
Ubicación: Barcelona - España
Posts: 19.507
Poder: 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:

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
          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
    // Crear el componente (usando TWinControl) a partir de la clase
    comp       := TWinControl(CompClass.Create(Self));       
    comp.Name  := comp.ClassName + IntToStr(contcomp);
    comp.Parent:= Cat;
    comp.Tag   := iTag;
    comp.Width := iWidth;
    // comp.Text  := '';
    comp.Top   := tp;
    comp.Left  := iLeft;
    // RTTI para propiedades published
    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.
__________________
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
  #2  
Antiguo 19-10-2022
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 159
Poder: 15
steelha Va por buen camino
Ok probare. Gracias
Responder Con Cita
  #3  
Antiguo 20-10-2022
steelha steelha is offline
Miembro
 
Registrado: mar 2012
Posts: 159
Poder: 15
steelha Va por buen camino
Buenas, he copiado el codigo tal cual me indicas y agregado al use System.TypInfo. Pero primero tube que arreglar la linea donde dice
Código Delphi [-]
var sDatosTipo:string := Trim(DM.QryPButtons.FieldByName('DatosTipo').AsString);
me da error lo coloque como siempre hago
Código Delphi [-]
Procedure xxx
Var 
   StadosTipo : String;
begin
....
end;

Pero aun así consigo un error el el procedimiento de crear componente
[dcc32 Error] ufrmpacientes.pas(276): E2029 '(' expected but ':=' found

Código Delphi [-]
procedure TfrmPacientes.CreateComponent(CompClass: TComponentClass; iTag, iWidth, iLeft: integer; sCaption: string);
begin
    comp  := TWinControl(CompClass.Create(Self));
    comp.Name  := comp.ClassName + IntToStr(contcomp);
    comp.Parent:= Cat;
    comp.Tag   := iTag;
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Ayuda componente cmm07 Varios 6 01-12-2008 17:42:20
Ayuda con un componente. Lucciano Conexión con bases de datos 2 12-04-2006 18:57:04
Ayuda con componente CelestronFan Varios 6 09-10-2005 18:39:40
Ayuda con componente raco Internet 2 17-02-2005 02:59:50
Ayuda con componente animal Varios 2 16-02-2005 08:49:47


La franja horaria es GMT +2. Ahora son las 13:32:53.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi