Author: Tomas Rutkauskas
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.
1 procedure TForm1.MaskEditEnter(Sender: TObject);
2 begin3 if Sender is TMaskEdit then4 TMaskEdit(Sender).EditMask := '!999.99;1; ';
5 end;
6 7 procedure TForm1.MaskEditExit(Sender: TObject);
8 begin9 if Sender is TMaskEdit then10 TMaskEdit(Sender).Text := MyMoneyFormat(TMaskEdit(Sender).Text);
11 end;
12 13 function MyMoneyFormat(S: string): string;
14 var15 X: Integer;
16 R: string;
17 begin18 R := '0';
19 for X := 1 to Length(S) do20 if S[X] <> ' ' then21 R := R + S[X];
22 Result := Format('%0.002f', [StrToFloat(R)]);
23 end;