我写一个TEDIT的继承控件,我怎么处理使得别人只能输入数字?要加入什么消息处理?有哪 位好兄弟给出例子和代码?

rwq_ 2001-08-02 04:11:47
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
trainbox 2001-08-02
  • 打赏
  • 举报
回复
unit IntEdit;

interface

uses
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;

implementation

procedure 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.
zzutlink 2001-08-02
  • 打赏
  • 举报
回复
用maskedit控件!!!!!
不想用的话就在onkeypress中处理,输入一个截取一个,不是数字不让出来,哈哈!
lyre 2001-08-02
  • 打赏
  • 举报
回复
procedure CreateParam(var Params: TCreateParams); override;
.
.
.
procedure TMyEdit.CreateParam(var Params: TCreateParams);
begin
inherited;

with Params do
Style := Style or ES_NUMBER;
end;
willsound 2001-08-02
  • 打赏
  • 举报
回复
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;
kylix2001 2001-08-02
  • 打赏
  • 举报
回复
onKeypress or onKeydown 事件里面处理

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧