把下列语句写到keypress事件里:begin if not Key in ['0'..'9',#8,#13] then Key:=#0; end;
看我的这段代码,一定可以解决你的问题: procedure TFRM_JXLSZ.Edit3KeyPress(Sender: TObject; var Key: Char); begin if not ((key in ['0','1','2','3','4','5','6','7','8','9']) or (integer(Key)=8) or (integer(Key)=13)) then key:=#0; end;这段代码可以扩充,限制或开放任何按键 我的这段代码是可以输入数字、回车和退格键
在ONKEYPRESS下写 if Not Key in ['0'..'9'] then Key := #0
在ONKEYPRESS下写 if Not Key in ['0'..'9'] then Key := #0
我写的一个控件,很简单,没有处理小数点unit EditNUM;interfaceuses Windows, Messages, SysUtils, Classes, Controls, StdCtrls;type TEditNUM = class(TEdit) private { Private declarations } protected { Protected declarations } procedure KeyPress(var Key: Char); override ; public { Public declarations } published { Published declarations } end;procedure Register;implementationprocedure Register; begin RegisterComponents('Samples', [TEditNUM]); end;procedure TEditNUM.KeyPress(var Key: Char); begin //让编辑框只能输入数字 if Key in ['0'..'9', Chr(VK_BACK)] then begin ReadOnly := False ; end else begin ReadOnly := True ; end ; end;end.
if Not (Key ['0'..'9',#8,#32,#46,#32]) then Key := #0;
if not Key in ['0'..'9',#8,#13] then Key:=#0;
还有#13 if Key = #13 then NextContrl.SetFocus;
还有#13 if Key = #13 then NextContrl.SetFocus;
if not (Key in ['0'..'9',#8,#13]) then Key:=#0;只能写整数. 如果还要写实数 则写成: if not (Key in ['0'..'9','+','-','.','E','e',#8,#13]) then Key:=#0;再在onexit事件里写上:procedure TForm1.Edit1Exit(Sender: TObject); function IsFloat(const CheckString:string):Boolean; var v:Extended; begin Result:=TextToFloat(PChar(CheckString), v, fvExtended); end;begin if not isfloat((sender as TEDit).text) then Raise Exception.create('不是有效的实数'); end;
处理Edit的 KeyPress 事件 procedure TForm1Edit1KeyPress(Sender: TObject; var Key: Char); begin inherited; //使光标移到下一个控件,在回车的时候 if key = #13 then SelectNext(Sender as TWinControl,True,True); //限制输入 if not ((Key in ['0'..'9']) or (Key = #8)) then key := #0 ; end;
procedure TedtCardID.KeyPress(var Key:char); begin Inherited; if (Key>'9') or (Key in ['!'..'/']) then begin Key:=#0; end; end;
if not Key in ['0'..'9',#8,#13] then
Key:=#0;
end;
procedure TFRM_JXLSZ.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if not ((key in ['0','1','2','3','4','5','6','7','8','9']) or (integer(Key)=8) or (integer(Key)=13)) then
key:=#0;
end;这段代码可以扩充,限制或开放任何按键
我的这段代码是可以输入数字、回车和退格键
if Not Key in ['0'..'9'] then
Key := #0
if Not Key in ['0'..'9'] then
Key := #0
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;type
TEditNUM = class(TEdit)
private
{ Private declarations }
protected
{ Protected declarations }
procedure KeyPress(var Key: Char); override ;
public
{ Public declarations }
published
{ Published declarations }
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Samples', [TEditNUM]);
end;procedure TEditNUM.KeyPress(var Key: Char);
begin
//让编辑框只能输入数字
if Key in ['0'..'9', Chr(VK_BACK)] then
begin
ReadOnly := False ;
end
else
begin
ReadOnly := True ;
end ;
end;end.
Key := #0;
Key:=#0;
if Key = #13 then
NextContrl.SetFocus;
if Key = #13 then
NextContrl.SetFocus;
Key:=#0;只能写整数.
如果还要写实数
则写成:
if not (Key in ['0'..'9','+','-','.','E','e',#8,#13]) then
Key:=#0;再在onexit事件里写上:procedure TForm1.Edit1Exit(Sender: TObject);
function IsFloat(const CheckString:string):Boolean;
var
v:Extended;
begin
Result:=TextToFloat(PChar(CheckString), v, fvExtended);
end;begin
if not isfloat((sender as TEDit).text) then
Raise Exception.create('不是有效的实数');
end;
procedure TForm1Edit1KeyPress(Sender: TObject;
var Key: Char);
begin
inherited;
//使光标移到下一个控件,在回车的时候
if key = #13 then
SelectNext(Sender as TWinControl,True,True);
//限制输入
if not ((Key in ['0'..'9']) or (Key = #8)) then
key := #0 ;
end;
begin
Inherited;
if (Key>'9') or (Key in ['!'..'/']) then
begin
Key:=#0;
end;
end;