以下是我写的一个TimeEdit控件,基本没有什么问题。你只要稍作修改即可。
unit ZhdyTimeEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TZhdyTimeEdit = class(TCustomEdit)
private
{ Private declarations }
protected
{ Protected declarations }
procedure KeyPress(var Key: Char); override;
procedure KeyDown (var Key: Word; Shift: TShiftState);override;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
published
{ Published declarations }
property Text;
property onChange;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Zhdy Controls', [TZhdyTimeEdit]);
end;
{ TZhdyTimeEdit }
constructor TZhdyTimeEdit.Create(AOwner: TComponent);
begin
inherited;
Text:='00:00:00';
end;
procedure TZhdyTimeEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
if key=vk_delete then key:=0
else if key=vk_back then key:=0;
inherited;
end;
procedure TZhdyTimeEdit.KeyPress(var Key: Char);
var ss:integer;
begin
ss:=selstart;
if ss<=7 then sellength:=1;
if ss=0 then
begin
if not (key in ['0'..'2']) then key:='0';
end
else if ss=1 then
begin
if not (key in ['0'..'9']) then key:='0'
end
else if ss=2 then
begin
if not (key in [':']) then key:=':'
end
else if ss=3 then
begin
if not (key in ['0'..'5']) then key:='0'
end
else if ss=4 then
begin
if not (key in ['0'..'9']) then key:='0'
end
else if ss=5 then
begin
if not (key in [':']) then key:=':'
end
else if ss=6 then
begin
if not (key in ['0'..'5']) then key:='0'
end
else if ss=7 then
begin
if not (key in ['0'..'9']) then key:='0';
end
else if ss>=8 then
begin
key:=#0;
end;
inherited;
end;
end.