gpt4 book ai didi

delphi - DBDateTimePicker 验证

转载 作者:行者123 更新时间:2023-12-03 15:52:52 25 4
gpt4 key购买 nike

我下载了一个 DBDateTimePicker,我用它来编辑表中的日期、开始日期和完成日期。当附加到普通编辑框中的表格时,我在附加和发布之前使用以下代码,以防止在开始日期之前设置完成日期。

if  (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
begin

raise Exception.Create ('The Completion date of a Job can not be before a Start Date');
abort;
end;

问题是,我希望在通过 DBDateTimePickers 编辑日期时能够实现相同的目标,如果我在 BeforePost 事件或其他事件上有类似的代码,那么我将收到过早的错误消息,因为用户可能没有有机会编辑其他相关的 DBDateTimePicker。我不知道如何将这两个字段联系起来,以便我可以以某种方式将它们一起验证或提出问题的另一种解决方案,有什么建议吗?(我使用 adotable 连接访问,该组件是从以下链接下载的, http://apollosoft.net/jms/ )。

最佳答案

我猜您正在使用 TDBDateTimePicker来自this page但很难说,目前你的问题中缺少它。

但是您可以使用该字段的 OnValidate每当值更改时都会触发事件,但这是数据将要写入数据库的阶段,因此恕我直言,这是不必要的时间浪费。

您可以在尝试更新记录之前验证这些值,(几乎?)
当您尝试退出控件时,每个数据库感知控件的情况如何,因此您可以很容易地添加一些验证事件,例如日期时间选择器的 OnCanUpdate

由于缺少源链接,您从哪里获得组件,我假设您正在使用此 TDBDateTimePicker 。如果您将以下行添加到 DBDateTimePicker.pas 文件并在安装位置重建软件包,则新的 OnCanUpdate 事件将出现在组件的对象检查器中。如果你使用这个事件,它有一个重要的参数Allowed,如果你将其设置为True,你将允许组件更新记录,如果你将其设置为False(默认值),则不会有任何数据在您的数据集中更新。因此,您可以在此处进行验证并决定是否要更新记录。

type
TDBDateTimePicker = class;
TOnCanUpdate = procedure(Sender: TDBDateTimePicker;
var Allowed: Boolean) of object;
TDBDateTimePicker = class(TDateTimePicker)
private
FOnCanUpdate: TOnCanUpdate;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
published
property OnCanUpdate: TOnCanUpdate read FOnCanUpdate write FOnCanUpdate;
end;

procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
var
Allowed: Boolean;
begin
if Assigned(FOnCanUpdate) then
begin
Allowed := False;
FOnCanUpdate(Self, Allowed);
if not Allowed then
begin
SetFocused(True);
Exit;
end;
end;

try
FDataLink.UpdateRecord;
except
SetFocus;
raise;
end;
SetFocused(False);
inherited;
end;

因此,OnCanUpdate 事件中的代码可能如下所示(请注意,您可以将此事件用作两个日期时间选择器的通用事件)。

procedure TForm1.DTPAddStartDateCanUpdate(Sender: TDBDateTimePicker; 
var Allowed: Boolean);
begin
if (DTPAddStartDate.Date) > (DTPAddCompletionDate.Date) then
begin
// the Allowed parameter is in current code initialized to False
// when it comes into this event, so the following line has no
// sense here
Allowed := False;
// here you can display the error message or raise exception or just
// whatever you want, the record won't be modified in any way
Application.MessageBox('The completion date of a job cannot be before a ' +
'start date. The record won''t be modified ;-)', 'Date Input Error...',
MB_YESNO + MB_ICONSTOP + MB_TOPMOST);
end
else
// only setting the Allowed to True will actually allows the record to be
// updated, so if your validation passes, set the Allowed to True
Allowed := True;
end;

关于delphi - DBDateTimePicker 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10168431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com