这段代码看着改吧 try a:=strtoint(edit1.Text); except on EConvertError do showmessage('NOT A numeral');
to vavyboyleon 谢谢,,,
len:integer; len=length(trim(edit1.text)); for i=1 to len then begin if not (copy(trim(edit1.text,i,1))>='0' and copy(trim(edit1.text,i,1))<='9') then begin showmessage('这不是数字!!!'); exit; end; end; 思路差不多就这样,不过有一个函数可直接用来判断是不是数字,自己找找看看吧
在onkeypress事件中进行判断: if key in ['0'..'9'] then showmessage('这是数字') else showmessage('这不是数字')
如下方法可以判断是否是整数,但不可以判断是否是浮点数: n: Integer; n:= strToIntDef(Edit1.Text,-1); if n = -1 then ShowMessage('不是数字!'); 有个小问题,就是现在如果 Edit1.Text := '-1',则也认为不是数字,这可要自己控制了。 具体可参考function StrToIntDef(const S: string; Default: Integer): Integer;的帮助
vavyboyleon(一天到晚加班的驴) 你的方法倒是很有创意啊.哈哈
uses IdGlobal ... if IsNumeric(Edit1.Text) then ShowMessage('是数字!');
try
a:=strtoint(edit1.Text);
except
on EConvertError do
showmessage('NOT A numeral');
谢谢,,,
len=length(trim(edit1.text));
for i=1 to len then
begin
if not (copy(trim(edit1.text,i,1))>='0' and copy(trim(edit1.text,i,1))<='9') then
begin
showmessage('这不是数字!!!');
exit;
end;
end;
思路差不多就这样,不过有一个函数可直接用来判断是不是数字,自己找找看看吧
if key in ['0'..'9'] then
showmessage('这是数字')
else
showmessage('这不是数字')
n: Integer;
n:= strToIntDef(Edit1.Text,-1);
if n = -1 then
ShowMessage('不是数字!');
有个小问题,就是现在如果 Edit1.Text := '-1',则也认为不是数字,这可要自己控制了。
具体可参考function StrToIntDef(const S: string; Default: Integer): Integer;的帮助
...
if IsNumeric(Edit1.Text) then ShowMessage('是数字!');