mascara edit firemonkey

Nei junior dias queiroz

Nei junior dias queiroz Publicado 13/04/2018 


bom dia pessoal. criei um componente de mascara com o que achei na internet. esta funcionando normal. so que nao consigo passar um valor para ele so se eu digitar.

exemplo: MyMaskedit1.text:= '(11)1111-1111';

ele nao digita nd. Sera que alguem pode me ajudar so novato.


segue a unit abaixo:


unit uMyMaskEdit;


interface


uses

System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,

FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.MaskUtils,

FMX.Controls.Presentation, FMX.Edit, System.Character, FMX.StdCtrls;


type

TMascara = (Juridica,Fisica,Telefone,Celular,Cep);

TMyMaskEdit = class(TEdit)

private

FMascara: TMascara;

PhoneNumber: string;

FMyedit: Boolean;

procedure SetMascara(const Value: TMascara);

function GetCaretPos: Integer;

function GetMaxLength: integer;

procedure Validating(Sender: TObject; var Text: string);

procedure KeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);

procedure Enter(Sender: TObject);

procedure Typing(Sender: TObject);

procedure SetMyedit(const Value: Boolean);

{ Private declarations }

protected

{ Protected declarations }

public

{ Public declarations }

published

{ Published declarations }

property pessoa: TMascara read FMascara write SetMascara;

property Myedit: Boolean read Fmyedit write Setmyedit;

end;


procedure Register;


implementation

{$J+}

const Mask: string = '00.000.000/0000-00;0;*';

{$J-}

procedure Register;

begin

RegisterComponents('Samples', [TMyMaskEdit]);

end;


{ TMyMaskEdit }


procedure TMyMaskEdit.Enter(Sender: TObject);

begin

case FMascara of

Juridica: Mask:= '00.000.000/0000-00;0;*';

Fisica: Mask:= '000.000.000-00;0;*';

Telefone: Mask:= '(00)0000-0000;0;*';

Celular: Mask:= '(00)00000-0000;0;*';

Cep: Mask:= '00000-000;0;*';

end;

CaretPosition := GetCaretPos;

end;


function TMyMaskEdit.GetCaretPos: Integer;

var

i: integer;

begin

Result := 0;

for i := 0 to Mask.Length-1 do

begin

if not (MaskGetCharType(Mask, i) in [mcDirective, mcMask]) then

Result := Result + 1;

if (Result + PhoneNumber.Length) = i then

Break;

end;

Result := Result + PhoneNumber.Length - 1;

end;


function TMyMaskEdit.GetMaxLength: integer;

var

i: integer;

begin

Result := 0;

for i := 0 to Mask.Length-1 do

if (MaskGetCharType(Mask, i) in [mcMask]) then

Result := Result + 1;

end;


procedure TMyMaskEdit.KeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;

Shift: TShiftState);

begin

FormatMaskText(Mask, PhoneNumber);

if (Key = 8) and (PhoneNumber.Length > 0) then

PhoneNumber := Copy(PhoneNumber, 1, PhoneNumber.Length-1)

else

if (PhoneNumber.Length < GetMaxLength) and (Key = 0) and IsDigit(KeyChar) then

PhoneNumber := PhoneNumber + KeyChar

else

KeyChar := #0;

end;


procedure TMyMaskEdit.SetMyedit(const Value: Boolean);

begin

Fmyedit := Value;

OnKeyDown:= KeyDown;

OnTyping:= Typing;

OnValidating:= Validating;

OnEnter:= Enter;

end;


procedure TMyMaskEdit.SetMascara(const Value: TMascara);

begin

FMascara := Value;

case Value of

Juridica: Mask:= '00.000.000/0000-00;0;*';

Fisica: Mask:= '000.000.000-00;0;*';

Telefone: Mask:= '(00)0000-0000;0;*';

Celular: Mask:= '(00)00000-0000;0;*';

Cep: Mask:= '00000-000;0;*';

end;


end;


procedure TMyMaskEdit.Typing(Sender: TObject);

begin

CaretPosition := GetCaretPos;

end;


procedure TMyMaskEdit.Validating(Sender: TObject; var Text: string);

begin

Text := FormatMaskText(Mask, PhoneNumber);

end;


end.

 

Voltar ao topo