在keypress中加代码:
if not (key in ['0'..'9',#8]) then key:=#0; #8:退格键 #13:Enter键 #27:Esc键 #32:空格键 #10 换行 #48 0 #127 DEL
keyprees事件下
允許輸入’.’ ,’Enter’, ‘BackSpace’,’0…9 數字’
但 ‘.’只能有一個
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); var S : string; begin S := Edit1.Text; if ((key = char(8))or ((key >= char(48)) and (key <= char(57))) or (key = char(13)) or (key = ‘.’)) then begin if ((key = ‘.’) and (pos(‘.’,S) > 0)) then key := char(0); end else key := char(0); end;