Format EditBox on EditChange

I need to have a EditBox showing the Float format while typing.
I need it to be usefull for float values.
Expl:
Number=123456.789
to be shown as
123,456.789
I repeat : I need it to be shown while typing. If I enter manually this number in Edit4 and do this on a ButtonClick
var
  s:string;
begin
  s:=Edit4.Text;
  s:=StringReplace(s,',','', [rfReplaceAll])  ;
  Edit4.Text:=FormatFloat('#,##00.####',StrToFloat(s))  ;
  Edit4.SelStart := Length(Edit4.Text);
end;
then it works, but I need it to be on EditChange.
Any ideeas?
For integer values it works just fine on EditChange... but I am interested in floating point values.
LVL 11
calinutzAsked:
Who is Participating?
 
mikelittlewoodCommented:
I would also put in a

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Pos('.', Edit1.Text) > 0) and (key = '.') then
    key := #0;
end;

so that it isnt trying to duplicate decimal points
0
 
calinutzAuthor Commented:
I tryed it with TMaskEdit also but I am not satisfyed. Any other suggestions
0
 
mikelittlewoodCommented:
Any reason why it needs to be done within EditChange rather than key up/down/press?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
calinutzAuthor Commented:
Not really
0
 
pcsentinelCommented:
look at this link

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21398649.html#13845000

its not for the same format but could be adapted for it

regards
0
 
calinutzAuthor Commented:
No reason at all. I only need this Edit format to be shown to the user while he types...
That's all.
So I can use any event...
Thanks for the interest
0
 
mikelittlewoodCommented:
Try this, might need some slight adjustment

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  i, iLen: Integer;
  s: string;
begin
  i := 0;

  if Trim(Edit1.Text) <> '' then
  begin
    s := Trim(Edit1.Text);
    s := StringReplace(s,',','', [rfReplaceAll]);
    iLen:= Length(s);
    i := Pos('.', s);
    if i = iLen then
      s := Copy(s, 1, iLen-1);

    if Trim(s) <> '' then
      s := FormatFloat('#,###0.####',StrToFloat(s));

    if i = iLen then
      s := s + '.';

    Edit1.Text := s;
    Edit1.SelStart := Length(Edit1.Text);
  end;
end;
0
 
calinutzAuthor Commented:
Thanks. It works fine with minor adjustments.
0
 
mikelittlewoodCommented:
Ok great Calinutz, thought it might need a little bit of tinkering, but got the general idea down.

Glad I could help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.