procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (Key in ['0'..'9']) then begin Key := #0; ShowMessage('非法数字!'); end; end;
var I: Integer; E: Integer; begin Val(Edit1.Text, I, E); if E <> 0 then ShowMessage('以前的帖子问得太多了'); end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if (Ord(Key) <47) or (Ord(Key) > 58) then Key := #0; end;
以下这种做法,对于用户使用ctrl+C、V拷进来的数据就不能判断了procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if (Ord(Key) <47) or (Ord(Key) > 58) then Key := #0; end;
我刚写的一个函数,传入字符串,判断TURE/FALSE即可。 //判断字符串是不是数值 function IsNumber(s: string): boolean; var i, iLength: integer; begin iLength := Length(s); for i := 1 to iLength do begin if not (s[i] in ['0'..'9']) then begin Result := false; exit; end end; Result := true; end;
可以放在 onexit(失去焦点?) 事件中检测!
我给你一段源码,保证明没问题! 在onKeyPess事件中加入一下代码 if (Key < '0') or (Key > '9') then Key := Chr(0);
//or
TryStrToInt()
strtoint^
except
出错提示
end;
反正你要将edit中的字符串转化为数字的吗!
begin
if not (Key in ['0'..'9']) then
begin
Key := #0;
ShowMessage('非法数字!');
end;
end;
I: Integer;
E: Integer;
begin
Val(Edit1.Text, I, E);
if E <> 0 then
ShowMessage('以前的帖子问得太多了');
end;
begin
if (Ord(Key) <47) or (Ord(Key) > 58) then
Key := #0;
end;
begin
if (Ord(Key) <47) or (Ord(Key) > 58) then
Key := #0;
end;
strtoint(edit1.text)
except
showmessage('请输入整数');
end;
//判断字符串是不是数值
function IsNumber(s: string): boolean;
var
i, iLength: integer;
begin
iLength := Length(s);
for i := 1 to iLength do
begin
if not (s[i] in ['0'..'9']) then
begin
Result := false;
exit;
end
end;
Result := true;
end;
在onKeyPess事件中加入一下代码
if (Key < '0') or (Key > '9')
then Key := Chr(0);