L
lostlastlove
Unregistered / Unconfirmed
GUEST, unregistred user!
unit DateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;
type
TValidateErrorEvent = procedure (Sender: TObject
var Handled : Boolean) of object;
TDateEdit = class(TMaskEdit)
private
protected
FOnValidate : TNotifyEvent;
FOnValidateError : TValidateErrorEvent;
public
constructor Create(AOwner: TComponent)
override;
procedure ValidateEdit
override;
published
property OnValidate : TNotifyEvent read FOnValidate write FOnValidate;
property OnValidateError : TValidateErrorEvent read FOnValidateError write FOnValidateError;
end;
procedure Register;
implementation
constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited;
EditMask :='!0000/00/00;1
';
{Font.Name:='宋体';
Font.Size:=9;}
end;
procedure TDateEdit.ValidateEdit;
var Handled : Boolean;
begin
if Assigned(OnValidate) then
OnValidate(self)
else
begin
if Text <> ' / / ' then
begin
try
Text := FormatDateTime('dd/mm/yyyy',StrToDate(Text));
except
begin
Handled := False;
if Assigned(OnValidateError) then
OnValidateError(self,Handled);
if not Handled then
Raise Exception.Create('Invalid input value, use escape key to abandon changes.');
end;
end;
end;
end;
end;
procedure Register;
begin
RegisterComponents('ControlX', [TDateEdit]);
end;
end.
上面是一个DateEdit控件的原代码,继承自TMaskEdit, 添加了两个事件:
OnValidate和OnValidateError,我在程序设计的过程中用到两个DateEdit控件,
由于程序的需要,我要检验输入的日期值的有效性,比如日期不能
不能为未来的日期,还有后面的日期必须大于前面的日期等情况,并且及时给出提示信息。
在程序中我必须提前捕获错误,并且将出错信息在状态栏显示,而不是跳出错误提示框。
实现代码:
{————————————————————————————————————————}
procedure TMainForm.edtStartDateValidate(Sender: TObject);
begin
inherited;
with (Sender as TDateEdit) do
begin
if strtoDate(Text)>Today then
begin
DispErr('Can''t input a future date!');
Setfocus;
abort;
end;
end;
end;
procedure TMainForm.edtEndDateValidateError(Sender: TObject;
var Handled: Boolean);
begin
inherited;
(Sender as TDateEdit).SetFocus
DispErr('Invalid Date!');
Handled:=true;
Abort
end;
{————————————————————————————————————————}
//Today:Tdatetime,为当前日期!
//DispErr(string:string)为我在基类中定义的一方法,它在状态栏显示string内容!
但是上面的代码只能提示‘Can't input a future date!’,而当输入非法日期如
‘01/22/2001’时则不能提示‘Invalid Date!’也就是edtEndDateValidateError事件里
的代码并没有执行!
请教各位,我该如何实现?
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;
type
TValidateErrorEvent = procedure (Sender: TObject
var Handled : Boolean) of object;
TDateEdit = class(TMaskEdit)
private
protected
FOnValidate : TNotifyEvent;
FOnValidateError : TValidateErrorEvent;
public
constructor Create(AOwner: TComponent)
override;
procedure ValidateEdit
override;
published
property OnValidate : TNotifyEvent read FOnValidate write FOnValidate;
property OnValidateError : TValidateErrorEvent read FOnValidateError write FOnValidateError;
end;
procedure Register;
implementation
constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited;
EditMask :='!0000/00/00;1
';
{Font.Name:='宋体';
Font.Size:=9;}
end;
procedure TDateEdit.ValidateEdit;
var Handled : Boolean;
begin
if Assigned(OnValidate) then
OnValidate(self)
else
begin
if Text <> ' / / ' then
begin
try
Text := FormatDateTime('dd/mm/yyyy',StrToDate(Text));
except
begin
Handled := False;
if Assigned(OnValidateError) then
OnValidateError(self,Handled);
if not Handled then
Raise Exception.Create('Invalid input value, use escape key to abandon changes.');
end;
end;
end;
end;
end;
procedure Register;
begin
RegisterComponents('ControlX', [TDateEdit]);
end;
end.
上面是一个DateEdit控件的原代码,继承自TMaskEdit, 添加了两个事件:
OnValidate和OnValidateError,我在程序设计的过程中用到两个DateEdit控件,
由于程序的需要,我要检验输入的日期值的有效性,比如日期不能
不能为未来的日期,还有后面的日期必须大于前面的日期等情况,并且及时给出提示信息。
在程序中我必须提前捕获错误,并且将出错信息在状态栏显示,而不是跳出错误提示框。
实现代码:
{————————————————————————————————————————}
procedure TMainForm.edtStartDateValidate(Sender: TObject);
begin
inherited;
with (Sender as TDateEdit) do
begin
if strtoDate(Text)>Today then
begin
DispErr('Can''t input a future date!');
Setfocus;
abort;
end;
end;
end;
procedure TMainForm.edtEndDateValidateError(Sender: TObject;
var Handled: Boolean);
begin
inherited;
(Sender as TDateEdit).SetFocus
DispErr('Invalid Date!');
Handled:=true;
Abort
end;
{————————————————————————————————————————}
//Today:Tdatetime,为当前日期!
//DispErr(string:string)为我在基类中定义的一方法,它在状态栏显示string内容!
但是上面的代码只能提示‘Can't input a future date!’,而当输入非法日期如
‘01/22/2001’时则不能提示‘Invalid Date!’也就是edtEndDateValidateError事件里
的代码并没有执行!
请教各位,我该如何实现?