peterhowells

OnKeyPress Fun Example

Mar 18th, 2022
1,834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.70 KB | None | 0 0
  1. type
  2.   TfrmMain = class(TForm)
  3.     edtName: TEdit;
  4.     procedure edtNameKeyPress(Sender: TObject; var Key: Char);
  5.   private
  6.     { Private declarations }
  7.   public
  8.     { Public declarations }
  9.   end;
  10.  
  11. var
  12.   frmMain: TfrmMain;
  13.  
  14. implementation
  15.  
  16. {$R *.dfm}
  17.  
  18. procedure TfrmMain.edtNameKeyPress(Sender: TObject; var Key: Char);
  19. begin
  20.   // Fun code to demonstrate Chr() and Ord() functions
  21.   // This line adds 1 to the ASCII character code that is typed
  22.   //Key := Chr(Ord(Key) + 1);
  23.  
  24.   // Code to prevent ASCII characters other than numbers from being typed
  25.   // Every character other than 0 to 9 is replaced with a null character
  26.   if (Ord(Key) < 48) or (Ord(Key) > 57) then Key := #0;
  27. end;
  28.  
  29. end.
Advertisement