如何由TEdit控件做一个只能输入数值的控件(50分)

L

liusj

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:

本人刚从VB加入DELPHI,想学做个控件。以前我做这个控件
只需5 分钟,没想Delphi 做控件折磨麻烦。。。
希望各位高手指教。

(最好 email to lshengjian@hpis.com)
 
这种问题也有
方法1:使用 TMaskEdit(在 additional页上)
这是一个可以屏蔽输入的 Edit控件,将 EditMask设成 '9999999'你需要的
最大位数即可, Easy or not?

方法2:
自己写 Coding

TNumericEdit = class (TCustomEdit)
private
procedure KeyPress(var Key: Char);override;
end;

procedure TNumericEdit.KeyPress(var Key:Car);
begin
case Key of
Chr('0')..Char('9'):
inherited; //只准输入 0-9字符,调用原来的处理程序
else
MessageBeep(0);
end;

btw:你原来用的是什么开发工具,需要五分钟 VB--
 
Sorry 方法三:这是你想要的,在edit的keypress下加上这个就可以:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9':
exit;
else
key:=#0;
end;

end;


 
相左程控件吗? Easy!
1. 在Delphi 的 Component 菜单选取 New Component
2. Ancestor type: TEdit
3. Class Name: TMyEdit
4. Unit file name: Any Name
5. OK

type
TMyEdit = class(TCustomEdit)
private
{ Private declarations }
protected
{ Protected declarations }
procedure KeyPress(var Key: char); override;
public
{ Public declarations }
published
{ Published declarations }
end;

implementation

procedure TMyEdit.KeyPress(var key: char);
begin
inherited heypress(key);
if not key in ['0'..'9'] then key:=#0;
end;

end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
895
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部