Ver Mensaje Individual
  #12  
Antiguo 04-11-2016
rmendoza83 rmendoza83 is offline
Miembro
 
Registrado: ago 2006
Posts: 50
Reputación: 18
rmendoza83 Va por buen camino
Cita:
Empezado por ecfisa Ver Mensaje
Hola.

No uso los componentes que mencionas pero tal vez esta opción, que almacena y por tanto "recuerda" el último formato elegido, te podría ser útil:
Código Delphi [-]
unit uCurrFormat;

interface

uses Classes;

type
  TCurrFormat  = class
  private
    FIniFileName  : string;
    FDefaultFormat: string;
    FGlobalFormat : string;
    function  GetGlobalFormat: string;
    procedure SetGlobalFormat( const StrFormat: string );
  public
    constructor Create;
    procedure SetDisplayFormat( TC: TComponent );
    property Format: string read FGlobalFormat write SetGlobalFormat;
  end;

var
  CurrFormat: TCurrFormat;

implementation

uses SysUtils, Forms, TypInfo, IniFiles, DB;

constructor TCurrFormat.Create;
begin
  FIniFileName   := ExtractFilePath(Application.ExeName) + 'IniFile.ini';
  FDefaultFormat := '#,##0.0000';
  FGlobalFormat  := GetGlobalFormat;
end;

function TCurrFormat.GetGlobalFormat: string;
var
  ini : TIniFile;
begin
  ini := TIniFile.Create( FIniFileName );
  try
    if not FileExists( FIniFileName ) then
    begin
      Result := FDefaultFormat;
      ini.WriteString( 'DisplayFormat', 'Format', Result );
    end
    else
      Result := ini.ReadString( 'DisplayFormat', 'Format', '' );
  finally
    ini.Free;
  end;
end;

procedure TCurrFormat.SetGlobalFormat( const StrFormat: string );
var
  ini : TIniFile;
begin
  ini := TIniFile.Create( FIniFileName );
  try
    FGlobalFormat := StrFormat;
    if not FileExists( FIniFileName ) then
      ini.WriteString( 'DisplayFormat', 'Format', FDefaultFormat )
    else
      ini.WriteString( 'DisplayFormat', 'Format', FGlobalFormat );
  finally
    ini.Free;
  end;
end;

procedure TCurrFormat.SetDisplayFormat( TC: TComponent );
var
  i, j: Integer;
  fld : TField;
  pNfo: PPropInfo;
begin
  for i := 0 to TC.ComponentCount-1 do
  begin
    if TC.Components[i] is TDataSet then
    begin
      for j := 0 to TDataSet( TC.Components[i] ).FieldCount-1 do
      begin
        fld := TDataSet( TC.Components[i] ).Fields[j];
        if fld is TCurrencyField then
          TCurrencyField(fld).DisplayFormat := FGlobalFormat;
      end;
    end
    else
    begin
      pNfo := GetPropInfo( TC.Components[i], 'DisplayFormat' );
      if Assigned( pNfo ) then
        SetStrProp( TC.Components[i], pNfo, FGlobalFormat );
    end;
  end;
end;

initialization
  if not Assigned ( CurrFormat ) then
    CurrFormat := TCurrFormat.Create;

finalization
  CurrFormat := nil;
end.

Ejemplo del uso:

DataModule,
Código Delphi [-]
unit Unit2;
...
implementation

uses uCurrFormat;

procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
  DataSet.Open;
  CurrFormat.SetDisplayFormat(DataModule1); // (*)
end;

Form,
Código Delphi [-]
...
var
  MainForm: TMainForm;

implementation

uses Unit2{ DataModule }, uCurrFormat;

procedure TMainForm.Button1Click(Sender: TObject);
var
  peso: string;
begin
  if CheckBox1.Checked then
    peso := '$ '
  else
    peso := '';
  CurrFormat.Format := peso +'#,##0.' + StringOfChar('0', SpinEdit1.Value);
  CurrFormat.SetDisplayFormat(DataModule1);
end;
...

Vista del ejemplo:


(*) En esta línea se aplica el formato almacenado a los componentes del contenedor (form, datamodule, ) , por lo que tendrías que usarla en todas aquellas unidades en que desees visualizar el formato.

Saludos

Pd: (Si tenes alguna dificultad para implementarlo, avisame y te adjunto el código fuente del ejemplo)
Interesante lo que propones, de verdad, aunque con lo que hice logre implementarlo sin ningun problema! como bien dije anteriormente solo me queda solucionar lo de rave reports!
Responder Con Cita