begin if not ((ord(Key)>=48) and (ord(Key)<=57) or (ord(Key)=8) or (ord(Key)=36) or (ord(Key)=35) or (ord(Key)=38) or (ord(Key)=40) or (ord(Key)=37) or (ord(Key)=39) or (ord(key)=46)) then begin showmessage('只可输入数值,不可输入其它字符!'); Key:=#0; end;
procedure CreateParam(var Params: TCreateParams); override; . . . procedure TMyEdit.CreateParam(var Params: TCreateParams); begin inherited; with Params do Style := Style or ES_NUMBER; end;
unit IntEdit;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TIntEdit = class(TCustomEdit) private { Private declarations } FValue,FMaxValue,FMinValue: Integer; FText: string; fInputError,fEnterKey,fMaxLength,fLostFocus: TNotifyEvent;//,fNumberChange procedure TextChange(Sender: TObject); protected { Protected declarations } procedure SetValue (Value: Integer); procedure SetMaxValue (Value: Integer); procedure SetMinValue (Value: Integer); procedure KeyPress(var Key: Char); override; procedure LostFocus(Sender: TObject); public { Public declarations } constructor Create(AOwner: TComponent);override; published { Published declarations } property Font; property MaxLength; property Value: Integer read FValue write SetValue default 0; property MaxValue: Integer read FMaxValue write SetMaxValue default 0; property MinValue: Integer read FMinValue write SetMinValue default 0; property OnInputError: TNotifyEvent read fInputError write fInputError; property OnEnterKey: TNotifyEvent read fEnterKey write fEnterKey; property OnMaxLength: TNotifyEvent read fMaxLength write fMaxLength; property OnLostFocus: TNotifyEvent read fLostFocus write fLostFocus; end;procedure Register;implementationprocedure Register; begin RegisterComponents('LCC', [TIntEdit]); end;constructor TIntEdit.Create(AOwner: TComponent); begin inherited Create(AOwner); Text:='0'; Font.Name:='宋体'; Font.Size:=11; OnChange:=TextChange; OnExit:=LostFocus; end;procedure TIntEdit.SetValue(Value: Integer); begin if FValue<>Value then begin FValue:=Value; Text:=IntToStr(Value); end; end;procedure TIntEdit.SetMaxValue (Value: Integer); begin if FMaxValue<>Value then begin FMaxValue:=Value; if FValue>Value then SetValue(Value); end; end;procedure TIntEdit.SetMinValue (Value: Integer); begin if FMinValue<>Value then begin FMinValue:=Value; if FValue<Value then SetValue(Value); end; end;//处理输入的字符 procedure TIntEdit.KeyPress(var Key: Char); begin if (Key=#13) and (Assigned(fEnterKey)) then fEnterKey(Self) //处理回车键 else if not ((Key In ['0'..'9']) or (Key=#8)) then //处理非数字字符 begin Key:=#0; if Assigned (fInputError) then fInputError(Self); end; end;//当输入改变时判断输入是否合法 procedure TIntEdit.TextChange(Sender: TObject); begin if Text<>'' then begin fValue:=StrToInt(Text); if fMaxValue<>0 then if fValue>fMaxValue then begin Text:=FText; fValue:=StrToInt(Text); end else FText:=Text; end; if (Text<>'') and ((Length(Text)=MaxLength) or ((StrToInt(Text)*10>MaxValue) and (fMaxValue>0))) and Assigned(fMaxLength) then fMaxLength(Self); end;procedure TIntEdit.LostFocus(Sender: TObject); begin if Text<>'' then begin FValue:=StrToInt(Text); if Assigned(fLostFocus) then fLostFocus(Self); end; end;end.
if not ((ord(Key)>=48) and (ord(Key)<=57) or (ord(Key)=8) or (ord(Key)=36) or (ord(Key)=35) or (ord(Key)=38) or (ord(Key)=40) or (ord(Key)=37) or (ord(Key)=39) or (ord(key)=46)) then
begin
showmessage('只可输入数值,不可输入其它字符!');
Key:=#0;
end;
.
.
.
procedure TMyEdit.CreateParam(var Params: TCreateParams);
begin
inherited; with Params do
Style := Style or ES_NUMBER;
end;
不想用的话就在onkeypress中处理,输入一个截取一个,不是数字不让出来,哈哈!
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TIntEdit = class(TCustomEdit)
private
{ Private declarations }
FValue,FMaxValue,FMinValue: Integer;
FText: string;
fInputError,fEnterKey,fMaxLength,fLostFocus: TNotifyEvent;//,fNumberChange
procedure TextChange(Sender: TObject);
protected
{ Protected declarations }
procedure SetValue (Value: Integer);
procedure SetMaxValue (Value: Integer);
procedure SetMinValue (Value: Integer);
procedure KeyPress(var Key: Char); override;
procedure LostFocus(Sender: TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
published
{ Published declarations }
property Font;
property MaxLength;
property Value: Integer read FValue write SetValue default 0;
property MaxValue: Integer read FMaxValue write SetMaxValue default 0;
property MinValue: Integer read FMinValue write SetMinValue default 0;
property OnInputError: TNotifyEvent read fInputError write fInputError;
property OnEnterKey: TNotifyEvent read fEnterKey write fEnterKey;
property OnMaxLength: TNotifyEvent read fMaxLength write fMaxLength;
property OnLostFocus: TNotifyEvent read fLostFocus write fLostFocus;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('LCC', [TIntEdit]);
end;constructor TIntEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Text:='0';
Font.Name:='宋体';
Font.Size:=11;
OnChange:=TextChange;
OnExit:=LostFocus;
end;procedure TIntEdit.SetValue(Value: Integer);
begin
if FValue<>Value then
begin
FValue:=Value;
Text:=IntToStr(Value);
end;
end;procedure TIntEdit.SetMaxValue (Value: Integer);
begin
if FMaxValue<>Value then
begin
FMaxValue:=Value;
if FValue>Value then SetValue(Value);
end;
end;procedure TIntEdit.SetMinValue (Value: Integer);
begin
if FMinValue<>Value then
begin
FMinValue:=Value;
if FValue<Value then SetValue(Value);
end;
end;//处理输入的字符
procedure TIntEdit.KeyPress(var Key: Char);
begin
if (Key=#13) and (Assigned(fEnterKey)) then fEnterKey(Self) //处理回车键
else if not ((Key In ['0'..'9']) or (Key=#8)) then //处理非数字字符
begin
Key:=#0;
if Assigned (fInputError) then fInputError(Self);
end;
end;//当输入改变时判断输入是否合法
procedure TIntEdit.TextChange(Sender: TObject);
begin
if Text<>'' then
begin
fValue:=StrToInt(Text);
if fMaxValue<>0 then
if fValue>fMaxValue then
begin
Text:=FText;
fValue:=StrToInt(Text);
end
else FText:=Text;
end;
if (Text<>'') and ((Length(Text)=MaxLength) or ((StrToInt(Text)*10>MaxValue) and (fMaxValue>0)))
and Assigned(fMaxLength) then
fMaxLength(Self);
end;procedure TIntEdit.LostFocus(Sender: TObject);
begin
if Text<>'' then
begin
FValue:=StrToInt(Text);
if Assigned(fLostFocus) then fLostFocus(Self);
end;
end;end.