2010. február 19., péntek

How to assign an EditMask of 000.00 to a TDBEdit


Problem/Question/Abstract:

I have a TDBEdit component linked to a string field. I have an assigned EditMask = '000.00' to capture a $ amount (I cannot make the field numeric). When the user enters 0.00, the data is being stored as '0 .00' in the table. What is the correct EditMask for such an entry? Or do I have to get the data from another property to store in the table?

Answer:

An EditMask can be awkward. I tend toward tidying up with custom formatting.

procedure TForm1.MaskEditEnter(Sender: TObject);
begin
  if Sender is TMaskEdit then
    TMaskEdit(Sender).EditMask := '!999.99;1; ';
end;

procedure TForm1.MaskEditExit(Sender: TObject);
begin
  if Sender is TMaskEdit then
    TMaskEdit(Sender).Text := MyMoneyFormat(TMaskEdit(Sender).Text);
end;

function MyMoneyFormat(S: string): string;
var
  X: Integer;
  R: string;
begin
  R := '0';
  for X := 1 to Length(S) do
    if S[X] <> ' ' then
      R := R + S[X];
  Result := Format('%0.002f', [StrToFloat(R)]);
end;

Nincsenek megjegyzések:

Megjegyzés küldése