我正在使用Delphi 7代码,以确保在用户切换选项卡之前已保存在选项卡上输入的注释。
选项卡位于TPageControl
上,并且此代码被触发OnExit
procedure TfCallerInfo.tsChaplainExit(Sender: TObject);
begin
{ Compare the saved DB value with the text in the comments field }
if (dmMain.qChaplainCOMMENTS.AsString <> dbmChapComments.Text) then
begin
ShowMessage ('Please save the comments before proceeding.');
pcDetail.ActivePage := tsChaplain; // Remain on the Current Page
tsChaplain.SetFocus;
end;
end;
例如,当用户单击另一个选项卡
tsInfoRequest
时,将触发验证,但活动页面将变为tsInfoRequest
,而不是其余的tsChaplain
。知道我在做什么错吗?
最佳答案
可能有更好的方法来做您想做的事情。请使用TPageControl.OnPageChanging
事件。
procedure TfCallerInfo.pcDetailPageChanging(Sender: TObject;
NewPage: TTabSheet; var AllowChange: Boolean);
begin
if pc.ActivePage = tsChaplain then
begin
AllowChange := (dmMain.qChaplainCOMMENTS.AsString = dbmChapComments.Text);
if not AllowChange then
ShowMessage(...);
end;
end;
顺便说一句,更好的测试可能是
AllowChange := not dmMain.gChaplainCOMMENTS.Modified;
当数据集的字段内容更改为
TField.Modified
或True
模式时,dsEdit
设置为dsInsert
;当其状态更改回False
时,设置为dsBrowse
。