RAD Studio Here my sample for simulating a "TMaskEdit" (from VCL) in FireMonkey using "TEdit" (from FMX)

emailx45

Свой
Регистрация
5 Май 2008
Сообщения
3,582
Реакции
2,450
Credits
578
Here my sample for simulating a "TMaskEdit" (from VCL) in FireMonkey using "TEdit" (from FMX)
by Emailx45

Scenary:
  • RAD Studio 10.3.3 Rio
  • FireMonkey project
  • a idea on my mind







[SHOWTOGROUPS=4,20,22]

Код:
unit uFormMain;

interface

uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,
  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.Controls.Presentation,
  FMX.Edit,
  FMX.ScrollBox,
  FMX.Memo;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Edit1ChangeTracking(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit1Validating(Sender: TObject; var Text: string);
    procedure Edit1Validate(Sender: TObject; var Text: string);
  private
    procedure prcMyLog(lText: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

var
  lDecimalSeparator : Char = '.';
  lThousandSeparator: Char = '.';

function fncFormatValue(lText: string): string;
var
  lCharK : Char;
  lFormat: string;
begin
  lCharK := #0; // Char(0);
  //
  lText := UpperCase(StringReplace(lText, ' ', '', [rfReplaceAll]));
  lText := StringReplace(lText, lThousandSeparator, '', [rfReplaceAll]);
  //
  if lText.Contains('K') then
  begin
    lText  := StringReplace(lText, 'K', '', [rfReplaceAll]);
    lCharK := 'K';
  end;
  //
  // value will be: <= 999.999.999
  lFormat := Format('###%s###%s##0', [lDecimalSeparator, lDecimalSeparator]);
  lText   := FormatCurr(lFormat, StrToFloatDef(lText, 0.0));
  //
  result := lText + lCharK;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  // prcMyLog(Format('OnChange - %s', [DateTimeToStr(now)]));
end;

procedure TForm1.Edit1ChangeTracking(Sender: TObject);
begin
  // prcMyLog(Format('OnChangeTracking - %s - %s', [DateTimeToStr(now), (Sender as TEdit).Text]));
end;

procedure TForm1.Edit1Validate(Sender: TObject; var Text: string);
begin
  // if not valid, then, clear the text for example!
  Text := fncFormatValue(Text);
  prcMyLog(Format('OnValidate - %s - [%s]', [DateTimeToStr(now), Text]));
end;

procedure TForm1.Edit1Validating(Sender: TObject; var Text: string);
begin
  // prcMyLog(Format('OnValidating - %s - %s', [DateTimeToStr(now), Text]));
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  lFS: TFormatSettings;
begin
  Edit1.FilterChar   := '0123456789Kk.';
  Edit1.MaxLength    := 12;                              // '123.456.789K'
  lFS                := TFormatSettings.Create('pt-BR'); // <-- IMPORTANT!
  lDecimalSeparator  := lFS.DecimalSeparator;
  lThousandSeparator := lFS.ThousandSeparator;
end;

procedure TForm1.prcMyLog(lText: string);
begin
  if not(lText.Trim = EmptyStr) then
    Memo1.Lines.Add(lText);
end;

end.
[/SHOWTOGROUPS]
 
Последнее редактирование: