最简单的控件问题?

janb janb 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在一本书上看到一个“数字编辑框”的控件例子,就是智能输入数字,我照例子敲入了代码,编译通过,可是不能输入任何字符(包括数字),请高手看看这是什么原因?
源码如下:
unit NumEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TNumEdit = class(TCustomEdit)
private
{ Private declarations }
fInputError:TNotifyEvent;
protected
{ Protected declarations }
function GetValue:Integer;
Procedure SetValue(value:Integer);
public
{ Public declarations }
Procedure WmChar(var Msg:TWmChar);message wm_Char;
constructor Create(Owner:TComponent);override;
published
{ Published declarations }
Property OnInputError:TNotifyEvent
read fInputError Write fInputError;
Property value:integer
read GetValue write SetValue;
property AutoSelect;
Property AutoSize;
Property BorderStyle;
Property CharCase;
Property Color;
Property Ctl3D;
Property DragCursor;
Property DragMode;
Property Enabled;
Property Font;
Property HideSelection;
Property MaxLength default 8;
Property OEMConvert;
Property ParentColor;
Property ParentCtl3D;
Property ParentFont;
Property ParentShowHint;
Property PasswordChar;
Property PopupMenu;
Property ReadOnly;
Property ShowHint;
Property TabOrder;
Property TabStop;
Property Visible;
Property onChange;
Property onClick;
Property OnDblClick;
Property OnDragDrop;
Property OnDragOver;
Property OnEndDrag;
Property OnEnter;
Property OnExit;
Property OnKeyDown;
Property OnKeyPress;
Property OnKeyUp;
Property OnMouseDown;
Property OnMouseMove;
Property OnMouseUp;
end;

procedure Register;

implementation


{ TNumEdit }

constructor TNumEdit.Create(Owner: TComponent);
begin
inherited Create( OWner);
MaxLength:=8;
Value:=0;
end;

function TNumEdit.GetValue: Integer;
begin
Result:=StrToIntDef(Text,0);
end;

procedure TNumEdit.SetValue(value: Integer);
begin
Text:=IntToStr(Value);
end;

procedure TNumEdit.WmChar(var Msg: TWmChar);
begin
if not (Char (Msg.CharCode) in ['0'..'9']) and not (Msg.charCode=8) then
begin
Msg.CharCode:=0;
if Assigned(fInputError) then
fInputError(Self);
end;
end;

procedure Register;
begin
RegisterComponents('MyComponent', [TNumEdit]);
end;

end.
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
janb
Delphi 5,945 成员 262,941 内容