如何让Edit只能输入数字?用MaskEdit好像只能输入规定的位数。
解决方案 »
- BrightnessChange(form1.Image1.Picture.Bitmap, TrackBarBrightnessChange.value, form1.ProgressBar1);
- 如何动态显示DBGRID内的某一列,平时是隐藏的,要在DBGrid1ColExit内写代码,
- 类似QQ的信息提示窗体怎么实现?
- 车丢了,散分
- 利用循环来进行计时的时候,为什么只在最后一轮有效?
- spinedit异常的问题
- 请教对Table操作时的问题
- 请问编程高手:安装程序中 ini文件是如何写成的,注册又是如何写成的??? 谢谢那位热心朋友耐心给我讲解好么?
- 一个在csdn一个多月的问题。。。
- RichEdit插入较多GIF动画后的闪烁问题
- excel问题!!!!
- 能说一下ASP中的ADO访问数据库和Delphi中的ADO访问有什么区别和相同之处。我能适应这些异同吗?
2在keypress中判断
通过keypress事件过滤非数字
通过keypress事件过滤非数字
if not (key in ['0'..'9',#13,#8] then
showmessage('');
如果想规定输入几个数字的话可以在属性里面设置length为n。
begin
if not (Key in ['0'..'9', #9, #13, #8]) then Key := #0;
//other code
end;
i:Byte;
begin
i:=strtoint(Edit1.text);
if (i in [0..65535]) then
showmessage('')
else
showmessage();
end;
比如,楼上关于KeyPress过滤,如果我输入负号'-'或者'2e3'或者'.1',算不算数字呢?
如果输入'-1-1'又怎么办呢?如果要用方向键移动光标呢?...
只允许输入整数(又分为是否运行小于0);
允许输入整数和实数(又分为是否运行小于0);
...
需要考虑的太多了。
因此,俺通常在用户输入确认时再检查是否合法(用try...except),省去一大堆麻烦。
if not (Key in ['0'..'9', #13, #8]) then Key := #0;
即可
#13,#8表可回车可退格要是没#8会不能删自符
begin
if not (Key in ['0'..'9', #13, #8]) then
Key := #0;end;
begin
if not (Key in ['0'..'9', #9, #13, #8]) then Key := #0;
//other code
end;
begin
if not (Key in ['0'..'9', #13, #8]) then
Key := #0;end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9', #13, #8]) then
Key := #0;end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not isnumberic(key) then
begin
showmessage('提示信息');
key:=char(0);
end;
end;
同时可以单独加入判断回车等其他键;isnumberic()是系统提供的函数;