1

I am trying to writte a code in Delphi, which will be displaying a given number in currency format like this:

enter image description here

Now it works if filled whole number and another field until any paramater is updated. I'd be happy to have a code which would displaying a number in currency format as I writte it. I've tried to manage it by typing this:

procedure TForm1.EditZakladChange(Sender: TObject);
var zaklad: Currency;
begin
  zaklad := StrToFloat(EditZaklad.Text);
  EditZaklad.Text := FloatToStrF(zaklad, ffCurrency, 10, 2);
end;

end.

But everytime when someting is typed in (zaklad field), this error appears:

enter image description here

Could somebody help me with that, please?

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • You can't really hope to do this. An OnChange handler that modifies the edit to which it is attached. Too incestuous. Perhaps you need a masked edit. – David Heffernan Jun 20 '15 at 10:41
  • Thak you for your reply David! How should I employ the masked edit? I am new to Deplhi and have no experience with that. –  Jun 20 '15 at 11:04
  • I don't know. I'm not a user of masked edits. I have a feeling they may be too restrictive though. – David Heffernan Jun 20 '15 at 11:19
  • In addition to what @DavidHeffernan said, you have another issue (source of the error you are seeing) and that is you cannot convert string containing thousands separators and currency to float. Not even `StrToCurr` supports that kind of conversion – Dalija Prasnikar Jun 20 '15 at 12:28
  • Can I handle it anyway? –  Jun 20 '15 at 14:51

1 Answers1

0

Make sure the MaxLength of edtZaklad is 0.

var
  Form21: TForm21;
  Check:string; //to break the loop.

function GetCurrency(Num: String):string;
var
  i: Integer ;
  Str:String;
  zaklad: Currency;
begin
  Result := '';
  for i := 1 to length(Num) do
  begin
    if (Num[i] in ['0'..'9']) then
    Begin
      Str := Str + Num[i] ;
      zaklad := StrToFloat(Str);
      Result := FloatToStrF(zaklad, ffCurrency, 10, 2);
      Check := Result;
    End;
  end;
end;

function ExtractCurrencySymbol(zaklad: String):string;
var
  i: Integer ;
  Str:String;
begin
  Result := '';
  for i := 1 to length(zaklad) do
  begin
    if NOT (zaklad[i] in ['0'..'9','.',',']) then
    Begin
      Str := Str + zaklad[i];
      Result := ' '+Trim(Str);
  end;
  end;

end;

procedure TForm21.EditZakladChange(Sender: TObject);
begin
  if AnsiPos(Check,EditZaklad.Text) <> 0 then exit;
  if (Trim(EditZaklad.Text) = '') then exit
  else
  begin
    EditZaklad.Text := GetCurrency(EditZaklad.Text)+ExtractCurrencySymbol(EditZaklad.Text);
    Exit;
  end;
end;

I hope this what you asked for.

RepeatUntil
  • 2,272
  • 4
  • 32
  • 57
  • 1
    So I can write `1A2B3C` and you return `123` ? That's far from what is asked (the problem was, except others, the locale currency symbol in the input string). Except that you are not considering negative values. Except that you are formatting the just concatenated output string inside a loop. – TLama Jun 20 '15 at 20:25
  • @TLama Answer is updated, My English is not that good. i hope i was answered what he asked for now. – RepeatUntil Jun 21 '15 at 02:03
  • 1
    The currency symbol is stored by the `TFormatSettings.CurrencyString` member. And you're still feeding the `Result` variable with a formatted text at every iteration. And that `Check` global variable remove until someone notice and start to downvote ;-) Parsing text back to what `CurrToStr` outputs is not that easy. Oh, and there is still no negative number consideration. I would simply let the users enter their values as floating point numbers into an edit box and currency symbol display in a label. – TLama Jun 21 '15 at 09:38
  • I am sorry my incorectly added post, I am new to StackOverflow forum. Here's the shorted previous post: Thank you very much for your reply, Abdulrahman Aljehani. That's excatly what I was looking for and I've learned from your code a lot! Unfortunetly, I am getting this error message: [Error] UrokovaKalkulacka.pas(74): Undeclared identifier: 'OnChange' I can't see nothing wrong with the procedure (new to Deplhi also) and don't know how to make it work. Any futher help would be appreciate. –  Jun 21 '15 at 14:57
  • 1
    @Stanislav If you don't understand that error then you aren't ready to take on this work. You need to go back to the language basics. – David Heffernan Jun 21 '15 at 16:32
  • I wonder how I could forget to declared it :-) Otherwise, it do nothing at all. –  Jun 21 '15 at 21:00