如何写一个编辑框控件?(50分)

  • 主题发起人 主题发起人 delphilai
  • 开始时间 开始时间
D

delphilai

Unregistered / Unconfirmed
GUEST, unregistred user!
第一次写控件,不知道如何写?
写一个继承自Edit编辑框的控件,要求该编辑框只能接收数字或“.”,如何重载DoKeydown事件?
不能用override一个静态的DokeyDown事件。

unit EditDigit;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
TEditDigit = class(TEdit)
private
{ Private declarations }
protected
{ Protected declarations }
OnKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);override;
public
{ Public declarations }
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TEditDigit]);
end;

procedure OnkeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
inherited;
if not (Key in ['0'..'9']) then
key:=0;
end;

end.
具体怎么实现?
 
如果你只是达到输入数字的效果,建议你重载KeyPress。
代码如下,测试通过:

unit EditMy;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls;
type
TEditMy = class(TEdit)
private
{ Private declarations }
CharSet: set of Char;
protected
{ Protected declarations }
procedure KeyPress(var Key: Char); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TEditMy]);
end;

constructor TEditMy.Create(AOwner: TComponent);
begin
inherited;
CharSet:=['0'..'9','.'];
end;

procedure TEditMy.KeyPress(var Key: Char);
begin
if (not (Key in CharSet)) and (Key<>#8) then
begin
Key:=#0;
Beep();
end;
inherited ;
end;

end.
 
关键是
if (not (Key in ['0'..'9','.'])) then
begin
Key:=#0;
Beep();
end;
inherited ;
 
接受答案了.
 
后退
顶部