在业务场景中我们需要录入年级,月份、年份等信息只能是正整数不能负数或者其它,我们对相应控件的KeyPress事件进行编写Key的约束就可以了;
procedure TForm2.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not(key in['0'..'9',#8])then
key:=#0;
end;
业务中对于采购金额等需要带小数的场景,同样进行如下约束
procedure TForm3.edt74KeyPress(Sender: TObject; var Key: Char); begin if not ((Key in ['0'..'9']) or ((key='.') and (Pos('.',edt74.Text)=0)) or (Key=chr(VK_Back))) then Key:=#0; end;