在edit的keypress事件里加入代码 if lenght(edit.text)=2 then key:=0;if not key in [1..9] then key:=0;
还有更好的 if (key<>#8)and (key<>#13) then if (key<#48) or (key>#57) or (length(editym.Text)=2) then key:=#0;
用maskedit的话,就无法让用户输入任意长的数字了……
if key in [#48..#57] then begin key:=#0; end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not(Key in ['0'..'9', #8]) then Key := #0;end;
看错了应该是if not (key in [#48..#57]) then
在 edit的 onkeypress事件里加入如下代码 不但可以控制在1-9中,而且可以有让edit的长度在2,还有backspace的删除功能。 上面的兄弟的代码不可以删除。 if (key<>#8)and (key<>#13) then if (key<#48) or (key>#57) or (length(edit.Text)=2) then key:=#0;
length(edit.Text)=2这个条件千万不能加啊,不然只能输入2个数字了!!!
if not (key in ['.','0'..'9'] )and(key<>#13) then begin Application.MessageBox('请输入数字','提示',mb_ok); key:=#0; end else if key=#13 then edit2.SetFocus;
#8是tab字符即chr(8); #13是回车字符即chr(13);
if not (key in ['.','0'..'9'] )and(key<>#13) then begin Application.MessageBox('请输入数字','提示',mb_ok); key:=#0; end else if key=#13 then edit2.SetFocus;
在keypress中加入如下语句 if not(key in ['0'..'9'] and key<>#13 and key<>#8 then abort;
定义一个过程: function strisnum(s:string):boolean; var i,j,l:integer; begin l:=length(s); j:=1; if (s[j] in ['+','-','0'..'9']) then //判断+ -号是否在第一位 begin for i:=2 to l do begin if s[i] in ['.','0'..'9']then result:=true else result:=false; end; end else result:=false; end;然后再在edit的keypress事件中加入 if key=#13 then begin if strisnum(edit1.text)=false then begin showmessage('无效的输入'); edit1.text:=''; edit1.setfocus; exit; end; end;
对'Backspace'和'Delete'等按键呢?
if length(edit_year3.text)=4 then key:=#0;if not (key in ['0'..'9']) then key:=#0;
if not(key in['0'..'9',#8,#13]) then //若输入的不是数字键、删除键或回车键 begin Beep; //发声警告 Key:=#0; //删除输入的非法字符 end;
在edit的onkeypress事件中输入 if Not(key in['0'..'9']) and (Key<>chr(vk_back)) then key:=chr(0); 一切OK了,这样只能输入数字,而且还可以用Backspace! 试试吧!
在edit的onkeypress事件中输入 if Not(key in['0'..'9','.',#8]) then key=#0
在edit的onkeypress事件中输入 if not (Key in ['0'..'9','.',#8]) then Key := #0;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not(Key in ['0'..'9', #8]) then Key := #0;end;
小弟只认为btbrave(彩旗飘飘)的方法是真正最可行的!修改Edit的Style!!!
//只输入数字如下: if ((key<#48) or (key>#57)) and (key<>#8) then abort;在OnkeyPress事件中加如下代码 //可以输入小数且只能有一个小数点如下(限制了输入负数): if ((key<#48) or (key>#57)) and (key<>#8) and (key<>#46) then abort; else if key=#46 then if StrScan(pchar(TEdit(Sender).text),'.')<>NIl then abort;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if Not (Key in ['1','2','3','4','5','6','7','8','9','0',#8]) then Key:=#0; end;
to fencher(凌):为什么不写成['0'..'9', #8]
edit-keypress() if ((keyascii<48) or (keyasc>57)) and (keyascii<>8) and (keyascii<>46) then keyascii=0 end if end sub “48是1的ascii码,57是9的ascii码,46是.的ascii码”
vb: private sub text_keypress(ascii as integer) if instr(chr(ascii),"1234567890")=0 then ascii=0 end sub
if lenght(edit.text)=2 then
key:=0;if not key in [1..9] then
key:=0;
if (key<>#8)and (key<>#13) then
if (key<#48) or (key>#57) or (length(editym.Text)=2)
then
key:=#0;
begin
key:=#0;
end;
begin
if not(Key in ['0'..'9', #8]) then
Key := #0;end;
不但可以控制在1-9中,而且可以有让edit的长度在2,还有backspace的删除功能。
上面的兄弟的代码不可以删除。
if (key<>#8)and (key<>#13) then
if (key<#48) or (key>#57) or (length(edit.Text)=2)
then
key:=#0;
begin
Application.MessageBox('请输入数字','提示',mb_ok);
key:=#0;
end
else
if key=#13 then edit2.SetFocus;
#13是回车字符即chr(13);
begin
Application.MessageBox('请输入数字','提示',mb_ok);
key:=#0;
end
else
if key=#13 then edit2.SetFocus;
标 题: Re: 如何屏蔽掉非数字键输入?
发信站: BBS 水木清华站 (Sat Jan 5 15:32:06 2002)
往编辑框中输入内容有以下几种方法:
1.键盘直接输入
2.拷贝,粘贴的方式
3.鼠标输入
看了大家的做法,只能控制第一种方式,如果要三种方式都控制,必须要在
edit1.onchange事件中,一个字符一个字符的判断,挺麻烦的.
好了,都把大家说糊涂了,其实一个简单的方法,可以试一试:
SetWindowLong(Edit1.Handle, GWL_STYLE,GetWindowLong(Edit1.Handle, GWL_STYLE)
or ES_NUMBER);
if not(key in ['0'..'9'] and key<>#13 and key<>#8 then
abort;
function strisnum(s:string):boolean;
var
i,j,l:integer;
begin
l:=length(s);
j:=1;
if (s[j] in ['+','-','0'..'9']) then //判断+ -号是否在第一位
begin
for i:=2 to l do
begin
if s[i] in ['.','0'..'9']then
result:=true
else
result:=false;
end;
end
else
result:=false;
end;然后再在edit的keypress事件中加入
if key=#13 then
begin
if strisnum(edit1.text)=false then
begin
showmessage('无效的输入');
edit1.text:='';
edit1.setfocus;
exit;
end;
end;
key:=#0;if not (key in ['0'..'9']) then
key:=#0;
begin
Beep; //发声警告
Key:=#0; //删除输入的非法字符
end;
try
i:=strtoint(s);
except
Showmessage('only integer');
调试时不行,但脱离IDE就可以
s:=edit.text
try
i:=strtoint(s);
except
Showmessage('only integer');
调试时不行,但脱离IDE就可以
放在onchange里面
if Not(key in['0'..'9']) and (Key<>chr(vk_back)) then
key:=chr(0);
一切OK了,这样只能输入数字,而且还可以用Backspace!
试试吧!
if Not(key in['0'..'9','.',#8]) then
key=#0
if not (Key in ['0'..'9','.',#8]) then
Key := #0;
begin
if not(Key in ['0'..'9', #8]) then
Key := #0;end;
if ((key<#48) or (key>#57)) and (key<>#8) then
abort;在OnkeyPress事件中加如下代码
//可以输入小数且只能有一个小数点如下(限制了输入负数):
if ((key<#48) or (key>#57)) and (key<>#8) and (key<>#46) then
abort;
else
if key=#46 then
if StrScan(pchar(TEdit(Sender).text),'.')<>NIl then
abort;
begin
if Not (Key in ['1','2','3','4','5','6','7','8','9','0',#8]) then
Key:=#0;
end;
if ((keyascii<48) or (keyasc>57)) and (keyascii<>8) and (keyascii<>46) then
keyascii=0
end if
end sub
“48是1的ascii码,57是9的ascii码,46是.的ascii码”
private sub text_keypress(ascii as integer)
if instr(chr(ascii),"1234567890")=0 then ascii=0
end sub
要maskedit干什么?