Sounds like a job for a MaskEdit control actually.

Or have a look here:

http://delphi.about.com/library/weekly/aa070603a.htm

Stephen Posey
stephenlpo...@earthlink.net

-----Original Message-----
From: delphi-boun...@elists.org [mailto:delphi-boun...@elists.org] On Behalf Of 
Stephen Bowker
Sent: Thursday, June 11, 2009 2: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 -> Delphi@elists.org 
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi
******************************************************************
This email and any files transmitted with it from the El Paso
Corporation are confidential and intended solely for the
use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the
sender.
******************************************************************

_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to