我着有段程序,实现EDIT空间里只能输入数字
不过小数点也过滤掉了!你自己改改!
procedure TPMMS110.edtYearKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited;
if (key = 8) or (key = 37) or (key = 39) or (key = 46) or (key = 13) then
begin
TEdit(sender).readonly := false;
exit;
end;
if ((key >=48) and (key <= 57))
or ((key >= 96) and (key <= 105)) then
begin
TEdit(sender).readonly := false;
end
else
Edit(sender).readonly := true;
end;
try
i:=strtofloat(edit1.text);
showmessage('输入为数字);
except
if not((length(trim(edit1.text))=1) and ((trim(edit1.text)='.')
or (trim(edit1.text)='-') or (trim(edit1.text)='+'))) then
showmessage('输入不是数字);
end;
哈哈,又有分赚了!
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key<>#8) and (not(key in ['0'..'9']) and (key<>'.')) then
begin
key:=#0;
showmessage('只能输入数字!');
end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if pos('.',edit1.text)>0 then key:=#0;
if not(key in [#8,'0'..'9','.']) then key:=#0;
end;
change事件中写一句:if edit1.text='' then edit1.text:='0';
procedure Tform1.edit1keypress(.....);
var
i:integer
begin
//确保只能输入一个.号
for i := 0 to length(edit1.text) -1 do
if (key = #n) and (edit1.text = ".") then]
key := #0;
听panyongze的,或者,你可以利用strToFloat的异常写一个函数,来校验是否是实数
function IsFloat(str:string):boolean
var
realA:real;
begin
Result:=True;
try
realA:=strToFloat(str)
except
Result:=False;
end
end;
当然,在程序调试的时候,你要将Delphi的异常报告关掉