How about this solution. Use the Onchange Event to put this code in:
procedure TForm1.Edit1Change(Sender: TObject);
begin
If Pos('.',Edit1.Text) > 0 then DotVar := True //Global Var to check
for decimal point
else DotVar := False;
If DotVar = true then {check for more than one number past decimal
point}
If Length(Edit1.text) - Pos('.',Edit1.Text)> 1 then
Begin
Showmessage('Only one decimal point precision allowed');
Edit1.text := Copy(edit1.text,1,Length(Edit1.text)-1);
end;
end;
Be sure to create a global Boolean variable called DotVar to hold the
flag for checking the number of decimals.
Hope this helps!
Tom Nesler
Live Long, Code Well, and Prosper...V
-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Stephen Bowker
Sent: Thursday, June 11, 2009 3:12 AM
To: Delphi (E-mail)
Subject: only allow 1 decimal point in edit box
Hi,
I've got some code that I want to do some simple validation on an edit
field to only allow positive numeric value with only 1 decimal point.
The code I have is as follows:
procedure TfMachines.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
inherited;
if not((key>=#48) and (key<=#57)) then //0..9
if (key=#46) or (key=#44) then //'.' & ','
begin
// if (pos(#46,(Sender as Tedit).text)>0) or (pos(#44,(Sender as
Tedit).text)>0) then
if (pos(#46, Edit1.text)>0) or (pos(#44, Edit1.text)>0) then
key := #0
else
key := decimalseparator;
end
else if (key<>#8) then
key := #0;
end;
This code nearly works, but allows more than one decimal point, because
when I debug and set a watch on Edit1.text, it is always blank, even
after several key presses.
I tried an Application.ProcessMessages before checking keys pressed, but
that didn't work either.
Also, the line that is commented out (sender as Tedit) I tried, and that
gave an error 'Invalid class typecast' as soon as I key a decimal point.
Any help would be appreciated, thanks in advance.
Regards,
Steve Bowker
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi