在DELPHI里面控制Edit中只能输入数字
2011-06-28 15:50
330 查看
如何在delphi里面控制Edit只能输入数字
━━━━━━━━━━━━━━━━━━━━━━━━━━
if not (key in ['0'..'9',#8]) then key := #0;
只能输入汉字,而不能输入数字或其他字符
━━━━━━━━━━━━━━━━━━━━━━━━━━
在Edit的OnKeyPress事件中
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (Ord(Key)<127) or (Ord(Edit1.Text[1])>127) then
Key:=#0;
end;
要使一文本框中只可输入数字,而且当输入的数字错误时还可以通过Backspace键来修改.
━━━━━━━━━━━━━━━━━━━━━━━━━━
由于BackSpace的ASCII值是8,所以像以下这样即可
if (key<>#46) and ((key < #48) or (key > #57)) and (key <> #8) then
//如果输入不是数字或小数点(#46代表小数点)
begin
key:=#0; //取消输入的内容(#0代表空值)
showmessage('输入错误!请输入数字!'); //发出提示信息
end;
方法二:
if not (key in ['0'..'9',#13,#8 ,#46]) then
key := #0;
这样就可以了
只能输入数字,而不能输入其他字符
━━━━━━━━━━━━━━━━━━━━━━━━━━
edit 属性Maxlength 设置2;
在edit的onkeypress里
procedure Tmainform.editkeypress(sender:tobject;var key: char );
var
Uflag: integer;
begin
Uflag:=Tedit(sender).Tag;
if (not (key in ['1'..'9'])) and (not (key=#8)) then key:=#0;
end;
方法二:
edit的maxlength设置为2;
在edit的onkeypress事件内
procedure Ttbdlform.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key <> #9 then// #9 是空格,你可以查找下数字1/2/3是什么值
showmessage('请输入数字')
end;
只能输入数字和小数点
━━━━━━━━━━━━━━━━━━━━━━━━━━
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8,#32]) then
key:= #0;
end;
end.
但如果你想只输入数字而且有格式限制 那么你最好还是用第三方控件`
方法二:
可以在keypress里面加上如下代码,可以输入数字,并且可以使用退格删除数字,可以使用回车
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9', #8, #13, #27, '.' : ;
else
begin
MessageBox(Handle, '请输入数字', PChar('输入错误'), MB_OK + MB_ICONINFORMATION);
Key := #0;
end;
end;
end;
━━━━━━━━━━━━━━━━━━━━━━━━━━
if not (key in ['0'..'9',#8]) then key := #0;
只能输入汉字,而不能输入数字或其他字符
━━━━━━━━━━━━━━━━━━━━━━━━━━
在Edit的OnKeyPress事件中
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (Ord(Key)<127) or (Ord(Edit1.Text[1])>127) then
Key:=#0;
end;
要使一文本框中只可输入数字,而且当输入的数字错误时还可以通过Backspace键来修改.
━━━━━━━━━━━━━━━━━━━━━━━━━━
由于BackSpace的ASCII值是8,所以像以下这样即可
if (key<>#46) and ((key < #48) or (key > #57)) and (key <> #8) then
//如果输入不是数字或小数点(#46代表小数点)
begin
key:=#0; //取消输入的内容(#0代表空值)
showmessage('输入错误!请输入数字!'); //发出提示信息
end;
方法二:
if not (key in ['0'..'9',#13,#8 ,#46]) then
key := #0;
这样就可以了
只能输入数字,而不能输入其他字符
━━━━━━━━━━━━━━━━━━━━━━━━━━
edit 属性Maxlength 设置2;
在edit的onkeypress里
procedure Tmainform.editkeypress(sender:tobject;var key: char );
var
Uflag: integer;
begin
Uflag:=Tedit(sender).Tag;
if (not (key in ['1'..'9'])) and (not (key=#8)) then key:=#0;
end;
方法二:
edit的maxlength设置为2;
在edit的onkeypress事件内
procedure Ttbdlform.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key <> #9 then// #9 是空格,你可以查找下数字1/2/3是什么值
showmessage('请输入数字')
end;
只能输入数字和小数点
━━━━━━━━━━━━━━━━━━━━━━━━━━
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8,#32]) then
key:= #0;
end;
end.
但如果你想只输入数字而且有格式限制 那么你最好还是用第三方控件`
方法二:
可以在keypress里面加上如下代码,可以输入数字,并且可以使用退格删除数字,可以使用回车
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9', #8, #13, #27, '.' : ;
else
begin
MessageBox(Handle, '请输入数字', PChar('输入错误'), MB_OK + MB_ICONINFORMATION);
Key := #0;
end;
end;
end;
相关文章推荐
- delphi里面控制Edit只能输入数字
- 如何在delphi里面控制Edit只能输入数字
- android 中怎么控制EditText只能输入数字和字母?
- Delphi一行语句实现edit只能输入数字
- Delphi 中的Edit输入框如何限定只能输入七位数,,第一位为1到4的数字,第二位到第七位为日期格式取后六位,如:20060123取:060123,任何一位都
- delphi Edit 控制最大值,只能输入数字型 控制小数位数(转)
- GUI_如何控制在文本框里面只能输入数字字符案例
- Delphi中设置Edit只能输入数字
- android 利用正则表达式 控制edittext只能输入数字、英文、汉字
- Edittext属性(2)之控制输入类型只能为数字和字母
- android使用正则表达式控制EditText只能输入数字和英文
- Delphi edit只能输入数字和退格键
- Delphi Edit限制只能输入数字
- 图形界面中如何控制在文本框里面只能输入数字字符
- Delphi-Edit中只能输入数字且只能输入一个小数点
- js正则控制文本框只能输入数字以及小数
- 控制DataGridView某列只能输入数字
- js控制文本框只能输入中文、英文、数字与指定特殊符号
- jquery控制输入框只能从键盘输入数字
- delphi edit 只输入数字 只输入字母