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

Reply via email to