最简单的控件问题?

janb 2002-06-10 11:18:33
我在一本书上看到一个“数字编辑框”的控件例子,就是智能输入数字,我照例子敲入了代码,编译通过,可是不能输入任何字符(包括数字),请高手看看这是什么原因?
源码如下:
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.
...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
janb 2002-06-10
  • 打赏
  • 举报
回复
up
zhaixingzi 2002-06-10
  • 打赏
  • 举报
回复
procedure tform1.editkeypress(sender:tobject;var key:char);
begin
if not (key in['0'..'9',#8]) then
key:=#0;
end;
这个是在EDIT中增加一个KEYPRESS属性下写的代码
janb 2002-06-10
  • 打赏
  • 举报
回复
我跟踪了一下好像根本不执行GetValue和SetValue过程
ibicf 2002-06-10
  • 打赏
  • 举报
回复
说清楚点,你这好像是做了个组件。你的构造函数好象有问题,我觉得这个东东没法倒入delphi后托拽到form上吧!
Tiga 2002-06-10
  • 打赏
  • 举报
回复
if not (Char (Msg.CharCode) in ['0'..'9']) and not (Msg.charCode=8) then

改为

if not (Char (Msg.CharCode) in ['0'..'9',#8] then


因为and的原因。

或者改为

if (not (Char (Msg.CharCode) in ['0'..'9'])) and (not (Msg.charCode=8) ) then

试试
janb 2002-06-10
  • 打赏
  • 举报
回复
不行,这不是根本问题,好像这个NumEdit不能用一样,不接受任何字符
taxi 2002-06-10
  • 打赏
  • 举报
回复
procedure TNumEdit.WmChar(var Msg: TWmChar);
begin
if not (Char(Msg.CharCode) in ['0'..'9', #8] then
begin
Msg.CharCode:=0;
if Assigned(fInputError) then
fInputError(Self);
end;
end;

5,402

社区成员

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

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