//假如你有十个菜单 if (sMenuNo > 0) and (sMenuNo <= 10) then //处理你定义的过程 else messagebox('无效的菜单编号');
function isnum(str:string):boolean; begin try strtoint(str); result:=true; except result:=false; end; end;
//刚才那个不对.这个对! procedure TForm1.Button1Click(Sender: TObject); var sMenuNo: string; begin sMenuNo := edit1.Text; if (sMenuNo > '0' ) and (sMenuNo <= '10') then self.Caption := 'aaa' else Application.MessageBox ('无效的菜单编号', '错误', MB_OK); end;
楼上的怎么回了两次问题都回串了。 对于用strtoint(str)方法来捕捉一异常,我不太赞同,如果这个字符串过长的话, 这个方法就不行了。 最好是挨个检查 function isnum(str:string):boolean; var i:integer; begin for i:=1 to length(str) do if not (str[i] in ['0'..'9']) then begin result:=false; exit; end; result:=true; end;
if (sMenuNo > 0) and (sMenuNo <= 10) then
//处理你定义的过程
else
messagebox('无效的菜单编号');
begin
try
strtoint(str);
result:=true;
except
result:=false;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sMenuNo: string;
begin
sMenuNo := edit1.Text;
if (sMenuNo > '0' ) and (sMenuNo <= '10') then
self.Caption := 'aaa'
else
Application.MessageBox ('无效的菜单编号', '错误', MB_OK);
end;
对于用strtoint(str)方法来捕捉一异常,我不太赞同,如果这个字符串过长的话,
这个方法就不行了。
最好是挨个检查
function isnum(str:string):boolean;
var
i:integer;
begin
for i:=1 to length(str) do
if not (str[i] in ['0'..'9']) then
begin
result:=false;
exit;
end;
result:=true;
end;