Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Implementando función (https://www.clubdelphi.com/foros/showthread.php?t=88758)

wilcg 27-07-2015 22:37:37

Implementando función
 
Amigos del foro, estoy trabajando en una función que me permita manipular propiedades de controles diferentes como

TEdit
TcxCurrencyEdit
TsDateEdit

y no me esta resultando por eso necesito de ustedes aquí La función

Código Delphi [-]
procedure OnOfReadOnly(Ctrls: array of TWinControl; const Enabled: Boolean);
var
  i: Integer;
begin
  for i:= Low(Ctrls) to High(Ctrls) do
  begin
    // TEdit
    if Ctrls[i] is TEdit then
    begin
      if Enabled = True then
      begin
        Ctrls[i].Color := $00FEEAD3;
        Ctrls[i].ReadOnly := Enabled;
      end else
      begin
        Ctrls[i].Color := clWhite;
        Ctrls[i].ReadOnly := Enabled;
      end;
    end else
    begin
    // TcxCurrencyEdit
    if Ctrls[i] is TcxCurrencyEdit then
    begin
      // ....

    end else
    begin
    // TsDateEdit
    if Ctrls[i] is TsDateEdit then
    begin
      // ....

    end;
    end;
    end;
  end;
end;

y para utilizarlo sería así...

Código Delphi [-]
procedure TFProductos.Button1Click(Sender: TObject);
begin
  OnOfReadOnly([Edit1, cxCurrencyEdit1, TsDateEdit],True);
end;

Lo que quiero es cambiar el color del edit o el cxCurrencyEdit, etc y protegerlo contra escritura activando la propiedad
ReadOnly mediante una función.

Lepe 27-07-2015 22:51:21

Te faltaba por usar la otra alternativa de la sentencia if:
Código Delphi [-]
if  a then 
begin 

end
else if b then
begin 

end

También he cambiado el nombre (a mi parecer ambiguo) que tenía por uno más estandard. Y el primer parámetro que le paso es si estará habilitado o no, aunque estas cosas es más al gusto de cada uno.

Yo creo que mejora la legibilidad ya que lees "Establecer solo lectura: SI, a los controles siguientes...blah, blah".

Código Delphi [-]
procedure SetReadOnly(const Enabled: Boolean; Ctrls: array of TWinControl);
var
  i: Integer;
begin
  for i:= Low(Ctrls) to High(Ctrls) do
  begin
    // TEdit
    if Ctrls[i] is TEdit then
    begin
      if Enabled 
        then  Ctrls[i].Color := $00FEEAD3
        else  Ctrls[i].Color := clWhite;
      Ctrls[i].ReadOnly := Enabled;
    end 
    else if Ctrls[i] is TcxCurrencyEdit then
    begin
      // TcxCurrencyEdit
    end
    else if Ctrls[i] is TsDateEdit then
    begin
    // TsDateEdit
    end;
  end;
end;

y para utilizarlo sería así...

Código Delphi [-]
procedure TFProductos.Button1Click(Sender: TObject);
begin
  SetReadOnly(True, [Edit1, cxCurrencyEdit1, TsDateEdit]);
end;

Saludos

wilcg 28-07-2015 00:22:57

Gracias Lepe por tu ayuda, pero en lo que me refiero a que no me esta resultando es a estos errores que me salen.



en la sentecia
Código Delphi [-]
// TEdit
    if Ctrls[i] is TEdit then
el control no me reconoce que es un TEdit o TcxTextEdit, etc... es aqui que devo encontrar la manera de indicar de que control se trata.

dec 28-07-2015 00:43:27

Hola,

Tendrás que hacer un "cast" o moldear el control en cuestión, algo como:

Código Delphi [-]
TEdit(Ctrls[i]).Color := clRed;

No recuerdo si "TCustomEdit" ya implementa la propiedad "Color". Si los controles que usas heredan todos de "TCustomEdit" y este cuenta con la propiedad "Color", entonces podrías usar "TCustomEdit" en lugar de "TWinControl", lo que te ahorraría hacer el moldeado mencionado arriba.

wilcg 28-07-2015 00:59:39

Gracias amigos por contribuir, creo haber solucionado, de esta manera. Aquí la función

función
Código Delphi [-]
procedure TForm1.OnOfReadOnly(const Enabled: Boolean; Ctrls: array of TControl);
var
  i: Integer;
begin
  for i:= Low(Ctrls) to High(Ctrls) do
  begin
    // TEdit
    if TWinControl(Ctrls[i]) is TEdit then
    begin
      if Enabled
        then  TEdit(Ctrls[i]).Color := $00FEEAD3
        else  TEdit(Ctrls[i]).Color := clWhite;
      TEdit(Ctrls[i]).ReadOnly := Enabled;
    end
    // TcxCurrencyEdit
    else if TWinControl(Ctrls[i]) is TcxCurrencyEdit then
    begin
      if Enabled
        then  TcxCurrencyEdit(Ctrls[i]).Style.Color := $00FEEAD3
        else  TcxCurrencyEdit(Ctrls[i]).Style.Color := clWhite;
      TcxCurrencyEdit(Ctrls[i]).Properties.ReadOnly := Enabled;
    end
    // TsDateEdit
    else if TWinControl(Ctrls[i]) is TsDateEdit then
    begin
      if Enabled
        then  TsDateEdit(Ctrls[i]).Color := $00FEEAD3
        else  TsDateEdit(Ctrls[i]).Color := clWhite;
      TsDateEdit(Ctrls[i]).ReadOnly := Enabled;
    end;
  end;
end;
Para usarlo.
Código Delphi [-]
procedure TFProductos.Button1Click(Sender: TObject);
begin
  OnOfReadOnly(True, [Edit1, cxCurrencyEdit1, TsDateEdit1]);
end;
Para volver al estado anterior de los componentes simplemente el True por False.


La franja horaria es GMT +2. Ahora son las 11:10:10.

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