真是easy啊procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (key in(['0'..'9'])) then key := #13; end;
定义一个枚举类型,[0,1,2,3,4,5,6,7,8,9,.] 在OnKeyDown事件中判断,如果NOT IN [.....],就返回空
if not (key in(['0'..'9'])) then begin // end;
很简单啊. 增加一个edit的onkeypress事件代码如下: var l:boolean; begin l:=(key<#48)or(key>#57); if l then key:=#0; end;
最好把‘.'、'Spaceback'带上:if not (key in['0'..'9','.',#8]) then key:=#0;
同意楼上 procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if not (key in ['0'..'9','.',#8]) then key := #0; end;
//用我做的控件,Style属性 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.
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.