界面上有N个edit我想让每个edit都只能输入 0-9的数字,我又不想在每个edit的keypress里写代码,
我想做成一个过程,每个edit的keypress里调用这个过程就可以了。
要如何做啊?目前我的判断代码:
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9']) then
begin
Key := #0;
end;
end;
我想做成一个过程,每个edit的keypress里调用这个过程就可以了。
要如何做啊?目前我的判断代码:
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9']) then
begin
Key := #0;
end;
end;
var
i: integer;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TEdit then
TEdit(Components[i]).OnKeyPress := myKeyPress;
//......
end;//记得在类中声明
procedure TForm1.myKeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9']) then
begin
Key := #0;
end;
end;
2.全部选好后,按F11显示属性栏,在OnKeyPress事件中填入你的myKeyPress。注: 第二步实际上就是操作所有edit了。
请注明版本,在D7中无此属性
procedure TForm1.edt1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9']) then
begin
Key := #0;
end;
end;
其他的EDIT的KeyPress都引用edt1KeyPress
当然,如果版本支持3L和4L所说的属性设置那更方便了
type
TEdit = class(StdCtrls.TEdit)
private
procedure WMChar(var Message: TWMChar); message WM_CHAR;
end; TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TEdit }procedure TEdit.WMChar(var Message: TWMChar);
begin
if (Message.CharCode >= 48) and (Message.CharCode <= 57) then
inherited;
end;
procedure TEdit.WMChar(var Message: TWMChar);
begin
if ((Message.CharCode >= 48) and (Message.CharCode <= 57)) or
(Message.CharCode = VK_BACK) then
inherited;
end;