我正在使用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.ModifiedTrue模式时,dsEdit设置为dsInsert;当其状态更改回False时,设置为dsBrowse

10-08 04:48