如何限制一个edit控件中只能输入数字,急急急急急急急急急急急急
37 个解决方案
#1
在edit的onChange事件中输入以入代码;
var
v: double;
code: integer;
numstr: string;
begin
numstr:= edit.text;
val(numstr,v,code);
if code > 0 then
begin
if length(numstr)=1 then
Delete(numstr,code,1)
else if numstr <> '-' then//如果输入的第一字符不是'-'
Delete(numstr,code,1); end;
edit.text:= numstr;
end;
var
v: double;
code: integer;
numstr: string;
begin
numstr:= edit.text;
val(numstr,v,code);
if code > 0 then
begin
if length(numstr)=1 then
Delete(numstr,code,1)
else if numstr <> '-' then//如果输入的第一字符不是'-'
Delete(numstr,code,1); end;
edit.text:= numstr;
end;
#2
为什么不用dbedit,数据类型为数值型,自动不允许录入基他东西.
#3
这是我原来写的一个一个过程,只能输入数字键和字母键和“.”
{键盘输入控制}
procedure KeyControl(Keyset:Char);
var
handle:HWND;
flagNum,flagXX,flagDX:boolean;
begin
flagNum:=((Keyset>'9') or (Keyset<'0'));
flagXX:=((Keyset>'z') or (keyset<'a'));
flagDX:= ((Keyset>'Z') or (keyset<'A'));
if (flagNum and flagXX and flagDX and (Keyset<>#8) and (Keyset<>'.')) then
begin
messagebox(handle,pchar('该按键此处禁用!'),pchar('信息'),mb_ok);
Abort;
end;
end;
{键盘输入控制}
procedure KeyControl(Keyset:Char);
var
handle:HWND;
flagNum,flagXX,flagDX:boolean;
begin
flagNum:=((Keyset>'9') or (Keyset<'0'));
flagXX:=((Keyset>'z') or (keyset<'a'));
flagDX:= ((Keyset>'Z') or (keyset<'A'));
if (flagNum and flagXX and flagDX and (Keyset<>#8) and (Keyset<>'.')) then
begin
messagebox(handle,pchar('该按键此处禁用!'),pchar('信息'),mb_ok);
Abort;
end;
end;
#4
在onkeypress中输入
if (key>='0')or(key<='9') then
key:=#0;
if (key>='0')or(key<='9') then
key:=#0;
#5
呵呵,这种问题去搜一下就一大堆了
#6
同意big_worm(大青虫)
#7
`谢谢了
#8
to 大青虫
你说的方法不行
如果用户输错怎么办?
也不能删除
if not(key in ['0'..'9']) then
begin
if not(key =#8) then
begin
application.MessageBox(' 输入必须为数字 !','提示',MB_ICONWARNING+MB_OK);
key:=chr(0);
end;
end;
这样的话输入错误后还可以删除
你试一下
你说的方法不行
如果用户输错怎么办?
也不能删除
if not(key in ['0'..'9']) then
begin
if not(key =#8) then
begin
application.MessageBox(' 输入必须为数字 !','提示',MB_ICONWARNING+MB_OK);
key:=chr(0);
end;
end;
这样的话输入错误后还可以删除
你试一下
#9
procedure TQueryBilForm.ComPTextKeyPress(Sender: TObject; var Key: Char);
begin
if Assigned(CurField) and(CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
if not((key in ['0'..'9','.',#8])) then
key := #0
else
begin
if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
Key:=#0;
end;
end;
小数点只能有一个,可以backspace!
begin
if Assigned(CurField) and(CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
if not((key in ['0'..'9','.',#8])) then
key := #0
else
begin
if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
Key:=#0;
end;
end;
小数点只能有一个,可以backspace!
#10
补充上面的,
if not((key in ['0'..'9','.',#8]) or key =#13) then
if not((key in ['0'..'9','.',#8]) or key =#13) then
#11
用异常处理来做:
try
StrtoFloat(Edit1.Text);
except
On EConvertError do
ShowMessage('必须是数字呀!');
end;
try
StrtoFloat(Edit1.Text);
except
On EConvertError do
ShowMessage('必须是数字呀!');
end;
#12
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
#13
up
真是越来越完善了
真是越来越完善了
#14
IsNumberic()
#15
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (not(key in ['0'..'9'])) and (key<>#8) and (key<>#46)then
begin
beep;
key:=#0;
end;
end;
begin
if (not(key in ['0'..'9'])) and (key<>#8) and (key<>#46)then
begin
beep;
key:=#0;
end;
end;
#16
为什么不用
MarkEdit
inputMark:9 0到一个数字
inputMarK:0 一个数字
MarkEdit
inputMark:9 0到一个数字
inputMarK:0 一个数字
#17
不用这么麻烦呵,
设定一下TEdit的样式则可:
var dwStyle:LongInt;
dwStyle := GetWindowLong(Edit1.Handle, GWL_STYLE);
SetWindowLong(Edit1.Handle, GWL_STYLE, dwStyle or ES_NUMBER);
请试用我的软件
www.netgocn.com
设定一下TEdit的样式则可:
var dwStyle:LongInt;
dwStyle := GetWindowLong(Edit1.Handle, GWL_STYLE);
SetWindowLong(Edit1.Handle, GWL_STYLE, dwStyle or ES_NUMBER);
请试用我的软件
www.netgocn.com
#18
//判断字符串
function judtext(smsText:String):Boolean;
var
i:shortint; //循环
judnorm:smallint; //判断基准
begin
Result:=True;
for i:=1 to length(smsText) do
begin
judnorm:=Byte(smsText[i]); //取得每一个字符的asc码
if (judnorm<48) or (judnorm>57) then
begin
Result:=False;
break;
end;
end;
end;
function judtext(smsText:String):Boolean;
var
i:shortint; //循环
judnorm:smallint; //判断基准
begin
Result:=True;
for i:=1 to length(smsText) do
begin
judnorm:=Byte(smsText[i]); //取得每一个字符的asc码
if (judnorm<48) or (judnorm>57) then
begin
Result:=False;
break;
end;
end;
end;
#19
//当edit1中不是一个有效数字时,把它值赋为0
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit1.Text:=IntToStr(StrToIntDef(Edit1.Text,0));
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit1.Text:=IntToStr(StrToIntDef(Edit1.Text,0));
end;
#20
在Edit的OnKeyPress内
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
#21
我无话可说了
#22
我也无话可说了,搬个凳子坐下听吧!
#23
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if ((key<'0') or (key>'9')) and (key<>#13) and (key<>'.') and (key<>#8) then
begin
Application.MessageBox('','');
abort;
end;
end;
刚试过,很好用的。
多谢楼上各位的指点。
begin
if ((key<'0') or (key>'9')) and (key<>#13) and (key<>'.') and (key<>#8) then
begin
Application.MessageBox('','');
abort;
end;
end;
刚试过,很好用的。
多谢楼上各位的指点。
#24
学习。正找这个限制输入,但要能修改的。
谢谢,谢谢
谢谢,谢谢
#25
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
#26
onKeyPress(Sender: TObject; var Key: Char);
// BackSpace退格键 :#8
if not ((Key=#8)or((key>='0')and(key<='9'))) then
key := #0 ;
// BackSpace退格键 :#8
if not ((Key=#8)or((key>='0')and(key<='9'))) then
key := #0 ;
#27
搬个板凳来听讲
#28
procedure Tform1.edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8,#46]) then key:=#0;
end;
begin
if not (key in ['0'..'9','.',#8,#46]) then key:=#0;
end;
#29
楼上的对!
#30
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
#31
找凳子坐下听课!
#32
if Edit1.Text <> '' then
begin
s := Edit1.Text;
for i := 1 to Length(s) do
begin
if (s[i] in ['0'..'9']) then
Continue
else
begin
Edit1.SetFocus;
Edit1.SelectAll;
MessageDlg('Invalide Num!',mtInformation,[mboK],0);
exit;
end;
end;
end
else
begin
Edit1.SetFocus;
MessageDlg('No Num!',mtInformation,[mboK],0);
Exit;
end;
begin
s := Edit1.Text;
for i := 1 to Length(s) do
begin
if (s[i] in ['0'..'9']) then
Continue
else
begin
Edit1.SetFocus;
Edit1.SelectAll;
MessageDlg('Invalide Num!',mtInformation,[mboK],0);
exit;
end;
end;
end
else
begin
Edit1.SetFocus;
MessageDlg('No Num!',mtInformation,[mboK],0);
Exit;
end;
#33
OnKeyPress事件中处理:
if key not in ['0'..'9'] then
Key:= #0;
if key not in ['0'..'9'] then
Key:= #0;
#34
firetoucher的这一段很直接,要uses windows
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
不过这样也行:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in['0'..'9',#13,#8])
then
begin
key:=chr(0);
showmessage('输入错误,请输入数字');
end;
end;
其实楼上的已经不能再完善了,还不结贴!
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
不过这样也行:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in['0'..'9',#13,#8])
then
begin
key:=chr(0);
showmessage('输入错误,请输入数字');
end;
end;
其实楼上的已经不能再完善了,还不结贴!
#35
又来晚一步,咳
ffwin()的最好
ffwin()的最好
#36
在Edit的OnKeyPress内
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
不对,不对 belllab(bell) 的最好
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
不对,不对 belllab(bell) 的最好
#37
眼都花了,真长见识
#1
在edit的onChange事件中输入以入代码;
var
v: double;
code: integer;
numstr: string;
begin
numstr:= edit.text;
val(numstr,v,code);
if code > 0 then
begin
if length(numstr)=1 then
Delete(numstr,code,1)
else if numstr <> '-' then//如果输入的第一字符不是'-'
Delete(numstr,code,1); end;
edit.text:= numstr;
end;
var
v: double;
code: integer;
numstr: string;
begin
numstr:= edit.text;
val(numstr,v,code);
if code > 0 then
begin
if length(numstr)=1 then
Delete(numstr,code,1)
else if numstr <> '-' then//如果输入的第一字符不是'-'
Delete(numstr,code,1); end;
edit.text:= numstr;
end;
#2
为什么不用dbedit,数据类型为数值型,自动不允许录入基他东西.
#3
这是我原来写的一个一个过程,只能输入数字键和字母键和“.”
{键盘输入控制}
procedure KeyControl(Keyset:Char);
var
handle:HWND;
flagNum,flagXX,flagDX:boolean;
begin
flagNum:=((Keyset>'9') or (Keyset<'0'));
flagXX:=((Keyset>'z') or (keyset<'a'));
flagDX:= ((Keyset>'Z') or (keyset<'A'));
if (flagNum and flagXX and flagDX and (Keyset<>#8) and (Keyset<>'.')) then
begin
messagebox(handle,pchar('该按键此处禁用!'),pchar('信息'),mb_ok);
Abort;
end;
end;
{键盘输入控制}
procedure KeyControl(Keyset:Char);
var
handle:HWND;
flagNum,flagXX,flagDX:boolean;
begin
flagNum:=((Keyset>'9') or (Keyset<'0'));
flagXX:=((Keyset>'z') or (keyset<'a'));
flagDX:= ((Keyset>'Z') or (keyset<'A'));
if (flagNum and flagXX and flagDX and (Keyset<>#8) and (Keyset<>'.')) then
begin
messagebox(handle,pchar('该按键此处禁用!'),pchar('信息'),mb_ok);
Abort;
end;
end;
#4
在onkeypress中输入
if (key>='0')or(key<='9') then
key:=#0;
if (key>='0')or(key<='9') then
key:=#0;
#5
呵呵,这种问题去搜一下就一大堆了
#6
同意big_worm(大青虫)
#7
`谢谢了
#8
to 大青虫
你说的方法不行
如果用户输错怎么办?
也不能删除
if not(key in ['0'..'9']) then
begin
if not(key =#8) then
begin
application.MessageBox(' 输入必须为数字 !','提示',MB_ICONWARNING+MB_OK);
key:=chr(0);
end;
end;
这样的话输入错误后还可以删除
你试一下
你说的方法不行
如果用户输错怎么办?
也不能删除
if not(key in ['0'..'9']) then
begin
if not(key =#8) then
begin
application.MessageBox(' 输入必须为数字 !','提示',MB_ICONWARNING+MB_OK);
key:=chr(0);
end;
end;
这样的话输入错误后还可以删除
你试一下
#9
procedure TQueryBilForm.ComPTextKeyPress(Sender: TObject; var Key: Char);
begin
if Assigned(CurField) and(CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
if not((key in ['0'..'9','.',#8])) then
key := #0
else
begin
if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
Key:=#0;
end;
end;
小数点只能有一个,可以backspace!
begin
if Assigned(CurField) and(CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
if not((key in ['0'..'9','.',#8])) then
key := #0
else
begin
if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
Key:=#0;
end;
end;
小数点只能有一个,可以backspace!
#10
补充上面的,
if not((key in ['0'..'9','.',#8]) or key =#13) then
if not((key in ['0'..'9','.',#8]) or key =#13) then
#11
用异常处理来做:
try
StrtoFloat(Edit1.Text);
except
On EConvertError do
ShowMessage('必须是数字呀!');
end;
try
StrtoFloat(Edit1.Text);
except
On EConvertError do
ShowMessage('必须是数字呀!');
end;
#12
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
#13
up
真是越来越完善了
真是越来越完善了
#14
IsNumberic()
#15
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (not(key in ['0'..'9'])) and (key<>#8) and (key<>#46)then
begin
beep;
key:=#0;
end;
end;
begin
if (not(key in ['0'..'9'])) and (key<>#8) and (key<>#46)then
begin
beep;
key:=#0;
end;
end;
#16
为什么不用
MarkEdit
inputMark:9 0到一个数字
inputMarK:0 一个数字
MarkEdit
inputMark:9 0到一个数字
inputMarK:0 一个数字
#17
不用这么麻烦呵,
设定一下TEdit的样式则可:
var dwStyle:LongInt;
dwStyle := GetWindowLong(Edit1.Handle, GWL_STYLE);
SetWindowLong(Edit1.Handle, GWL_STYLE, dwStyle or ES_NUMBER);
请试用我的软件
www.netgocn.com
设定一下TEdit的样式则可:
var dwStyle:LongInt;
dwStyle := GetWindowLong(Edit1.Handle, GWL_STYLE);
SetWindowLong(Edit1.Handle, GWL_STYLE, dwStyle or ES_NUMBER);
请试用我的软件
www.netgocn.com
#18
//判断字符串
function judtext(smsText:String):Boolean;
var
i:shortint; //循环
judnorm:smallint; //判断基准
begin
Result:=True;
for i:=1 to length(smsText) do
begin
judnorm:=Byte(smsText[i]); //取得每一个字符的asc码
if (judnorm<48) or (judnorm>57) then
begin
Result:=False;
break;
end;
end;
end;
function judtext(smsText:String):Boolean;
var
i:shortint; //循环
judnorm:smallint; //判断基准
begin
Result:=True;
for i:=1 to length(smsText) do
begin
judnorm:=Byte(smsText[i]); //取得每一个字符的asc码
if (judnorm<48) or (judnorm>57) then
begin
Result:=False;
break;
end;
end;
end;
#19
//当edit1中不是一个有效数字时,把它值赋为0
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit1.Text:=IntToStr(StrToIntDef(Edit1.Text,0));
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit1.Text:=IntToStr(StrToIntDef(Edit1.Text,0));
end;
#20
在Edit的OnKeyPress内
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
#21
我无话可说了
#22
我也无话可说了,搬个凳子坐下听吧!
#23
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if ((key<'0') or (key>'9')) and (key<>#13) and (key<>'.') and (key<>#8) then
begin
Application.MessageBox('','');
abort;
end;
end;
刚试过,很好用的。
多谢楼上各位的指点。
begin
if ((key<'0') or (key>'9')) and (key<>#13) and (key<>'.') and (key<>#8) then
begin
Application.MessageBox('','');
abort;
end;
end;
刚试过,很好用的。
多谢楼上各位的指点。
#24
学习。正找这个限制输入,但要能修改的。
谢谢,谢谢
谢谢,谢谢
#25
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
#26
onKeyPress(Sender: TObject; var Key: Char);
// BackSpace退格键 :#8
if not ((Key=#8)or((key>='0')and(key<='9'))) then
key := #0 ;
// BackSpace退格键 :#8
if not ((Key=#8)or((key>='0')and(key<='9'))) then
key := #0 ;
#27
搬个板凳来听讲
#28
procedure Tform1.edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8,#46]) then key:=#0;
end;
begin
if not (key in ['0'..'9','.',#8,#46]) then key:=#0;
end;
#29
楼上的对!
#30
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
var l:boolean;
begin
l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
if l then key:=#0;
end;
#31
找凳子坐下听课!
#32
if Edit1.Text <> '' then
begin
s := Edit1.Text;
for i := 1 to Length(s) do
begin
if (s[i] in ['0'..'9']) then
Continue
else
begin
Edit1.SetFocus;
Edit1.SelectAll;
MessageDlg('Invalide Num!',mtInformation,[mboK],0);
exit;
end;
end;
end
else
begin
Edit1.SetFocus;
MessageDlg('No Num!',mtInformation,[mboK],0);
Exit;
end;
begin
s := Edit1.Text;
for i := 1 to Length(s) do
begin
if (s[i] in ['0'..'9']) then
Continue
else
begin
Edit1.SetFocus;
Edit1.SelectAll;
MessageDlg('Invalide Num!',mtInformation,[mboK],0);
exit;
end;
end;
end
else
begin
Edit1.SetFocus;
MessageDlg('No Num!',mtInformation,[mboK],0);
Exit;
end;
#33
OnKeyPress事件中处理:
if key not in ['0'..'9'] then
Key:= #0;
if key not in ['0'..'9'] then
Key:= #0;
#34
firetoucher的这一段很直接,要uses windows
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
不过这样也行:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in['0'..'9',#13,#8])
then
begin
key:=chr(0);
showmessage('输入错误,请输入数字');
end;
end;
其实楼上的已经不能再完善了,还不结贴!
Edit 中只输入数字
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or
ES_NUMBER);
不过这样也行:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in['0'..'9',#13,#8])
then
begin
key:=chr(0);
showmessage('输入错误,请输入数字');
end;
end;
其实楼上的已经不能再完善了,还不结贴!
#35
又来晚一步,咳
ffwin()的最好
ffwin()的最好
#36
在Edit的OnKeyPress内
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
不对,不对 belllab(bell) 的最好
if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键}
Key:=#0;
不对,不对 belllab(bell) 的最好
#37
眼都花了,真长见识