一个简单问题:如何使Edit框只允许写入数字
解决方案 »
- 重新开放ftp,已有资料20GB
- 怎样利用dblookupcombobox进行模糊查询?
- 给出圆心点坐标,角度和半径长度,如何画出这条半径线?
- 关于TreeView试例中在Delphi自带的Demo文件夹中有吗?
- 怎样用TlistView???
- 这是sql server的bug吗??
- 请问怎么样得到一个:saveas窗口呀?
- 我要使dbtext控件呈现3d效果,应该设置哪些属性??
- 如何用BDE连接数据库,读取数据库的值并显示出来和打印,请高手指点!!!
- 求一个比较两个字符串的函数
- 我是新手
- procedure gettablename(tablename:string;from1:TForm1);
begin
if not (key in(['0'..'9'])) then
key := #13;
end;
在OnKeyDown事件中判断,如果NOT IN [.....],就返回空
begin
//
end;
增加一个edit的onkeypress事件代码如下:
var l:boolean;
begin
l:=(key<#48)or(key>#57);
if l then key:=#0;
end;
key:=#0;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8]) then
key := #0;
end;
unit tzDigitEdit;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type TStyle = (esInteger,esText,esFloat,esCurrency); TtzDigitEdit = class(TEdit)
private
FStyle: TStyle;
procedure WMPaste(var Message: TMessage); message WM_PASTE;
procedure WMChar(var Message: TWMChar); message WM_CHAR;
procedure SetStyle(const value: TStyle);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
{ Public declarations }
published
property Style: TStyle read FStyle write SetStyle default esInteger;
{ Published declarations }
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('TZhuang', [TtzDigitEdit]);
end;{ TtzDigitEdit }
constructor TtzDigitEdit.Create(AOwner: TComponent);
begin
inherited;
Style:= esInteger;
Text:= '0';
end;procedure TtzDigitEdit.SetStyle(const value: TStyle);
begin
if FStyle <> value then begin
//设计期或运行期
if (csDesigning in ComponentState) or (ComponentState = []) then
case value of
esText:
Text:= '';
esInteger:
Text:= '0';
esFloat,esCurrency:
Text:= '0.0';
end;
FStyle:= value;
end;
end;procedure TtzDigitEdit.WMChar(var Message: TWMChar);
var s: string;
begin
if Style = esText then begin
inherited;
exit;
end; if Chr(Message.CharCode) in ['a'..'z','A'..'Z'] then
Message.CharCode:= 0
else begin
s:= Text;
inherited;
try
if Style = esInteger then
StrToInt(Text)
else if Style = esFloat then
StrToFloat(Text)
else if Style = esCurrency then
StrToCurr(Text);
except
Text:= s;
end;
end;end;procedure TtzDigitEdit.WMPaste(var Message: TMessage);
var
S: string;
begin
if Style = esText then begin
inherited;
exit;
end; S:= Text;
inherited;
try
if Style = esInteger then
StrToInt(Text)
else if Style = esFloat then
StrToFloat(Text) //有争议,转换结果是Extended型,到数据库里是否出错?
else if Style = esCurrency then
StrToCurr(Text);
except
Text:= S;
end;end;end.