Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Solo acceder a componentes de un TPanel (https://www.clubdelphi.com/foros/showthread.php?t=78841)

gcaffe 20-05-2012 21:11:03

Solo acceder a componentes de un TPanel
 
Hola:
En una ventana tengo varios Panels en uno de ellos hay varios componentes, los cuales debo colocar y modificar sus propiedades dependiendo del perfil del usuario. En definitiva se arman varias plantillas, las propiedades de los componentes estan en un TClientDataSet en memoria, para hacer esto utilizo el siguiente código: (el Nombre del panel es PanSrv y del ClientDataset es Mapa)

Código Delphi [-]
 
for i := 0 to ComponentCount - 1 do begin
   if Components[i].GetParentComponent.Name = 'PanSrv' then begin
       if (Components[i] is TLabel) then begin
         TLabel(Components[i]).Left := MapaIzq.Value;
         TLabel(Components[i]).Top := MapaArr.Value;
         TLabel(Components[i]).Caption := MapaTexto.Value;
         TLabel(Components[i]).Width := MapaAncho.Value;
         TLabel(Components[i]).Visible := MapaVisible.Value;
       end
       else if (Components[i] is TEdit) then begin
         TEdit(Components[i]).Left := MapaIzq.Value;
         TEdit(Components[i]).Top := MapaArr.Value;
         TEdit(Components[i]).Width := MapaAncho.Value;
         TEdit(Components[i]).Visible := MapaVisible.Value;
         TEdit(Components[i]).TabOrder := MapaOrden.Value;
       end;
   end;
end;

este código funciona perfectamente, pero como podrán comprobar necesito recorrer todos los componentes del TForm, y son muchos, lo cual es rápido pero no es lo mas eficiente, así que pense recorrer solo los componentes del TPanel y realice este código:

Código Delphi [-]
 
with PanSrv do begin
  for i := 0 to ComponentCount - 1 do begin
    if (Components[i] is TLabel) then begin
      TLabel(Components[i]).Left := MapaIzq.Value;
      TLabel(Components[i]).Top := MapaArr.Value;
      TLabel(Components[i]).Caption := MapaTexto.Value;
      TLabel(Components[i]).Width := MapaAncho.Value;
      TLabel(Components[i]).Visible := MapaVisible.Value;
    end
    else if (Components[i] is TEdit) then begin
      TEdit(Components[i]).Left := MapaIzq.Value;
      TEdit(Components[i]).Top := MapaArr.Value;
      TEdit(Components[i]).Width := MapaAncho.Value;
      TEdit(Components[i]).Visible := MapaVisible.Value;
      TEdit(Components[i]).TabOrder := MapaOrden.Value;
    end;
  end;
end;

Para mi sorpresa este código no funciona porque PanSrv.ComponentCount siempre devuelve cero :confused: , sencillamente no se ejecuta.

Alguien me puede decir que estoy haciendo mal.

Muchas gracias

ecfisa 20-05-2012 21:54:03

Hola gcaffe.

Para aclarar el concepto, revisá este [ hilo ].

Entonces podría ser:
Código Delphi [-]
  with PanSvr do
    for i := 0 to ControlCount - 1 do
    begin
      if (Controls[i] is TLabel) then
      begin
        TLabel(Controls[i]).Left := MapaIzq.Value;
        TLabel(Controls[i]).Top := MapaArr.Value;
        TLabel(Controls[i]).Caption := MapaTexto.Value;
        TLabel(Controls[i]).Width := MapaAncho.Value;
        TLabel(Controls[i]).Visible := MapaVisible.Value;
      end
      else if (Controls[i] is TEdit) then
      begin
        TEdit(Controls[i]).Left := MapaIzq.Value;
        TEdit(Controls[i]).Top := MapaArr.Value;
        TEdit(Controls[i]).Width := MapaAncho.Value;
        TEdit(Controls[i]).Visible := MapaVisible.Value;
        TEdit(Controls[i]).TabOrder := MapaOrden.Value;
      end;
    end;

Saludos.

gcaffe 20-05-2012 22:56:51

Hola ecfisa:

Muchas gracias por tu respuesta, la probé y funcionó.

Un saludo


La franja horaria es GMT +2. Ahora son las 07:55:57.

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