Arsip untuk Maret, 2008

Numerical Input in Delphi

Posted in Uncategorized dengan kaitan (tags) , , , , , on Maret 10, 2008 by islamixcity

If you want to limit the input of a TEdit to numerical strings only, you can discard the “invalid” characters in its OnKeyPress event handler. Let’s assume that you only want to allow positive integer numbers. The code for the OnKeyPress event handler is as follows:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  // #8 is Backspace
  if not (Key in [#8, '0'..'9']) then begin
    ShowMessage('Invalid key');
    // Discard the key
    Key := #0;
  end;
end;