2

在我正在处理的应用程序的一部分中,有一个表单控件在接收到 CMExit 消息时进行验证,这正是 Delphi 文档所说的这样做(此代码示例来自 Delphi 帮助文件):

procedure TDBCalendar.CMExit(var Message: TWMNoParams);
begin
 try
  FDataLink.UpdateRecord;                          { tell data link to update database }
 except
  on Exception do SetFocus;                      { if it failed, don't let focus leave }
 end;
 inherited;
end;

这样做的目的是在控件失去焦点时立即执行验证。因此,例如,如果我单击“确定”按钮,表单控件将失去焦点,该方法将运行,并且在异常时会将焦点设置回该表单控件。(因此,确定按钮上的“单击”事件永远不会通过,对话框也永远不会关闭)。

我遇到的问题是此表单控件位于模式对话框窗口内。单击确定确实会发送 CMExit 消息并导致记录更新(并进行验证)。但是,在表单控件中按 Enter 会导致模式对话框关闭而不发送 CMExit 消息。就好像表单控件永远不会“失去焦点”。这意味着不仅对话框在没有表单实际验证数据的情况下关闭,而且数据集也没有更新。

鉴于这个问题,我放置数据集更新/验证代码的最佳位置在哪里?我可以将它移到对话框表单本身并在 OnCloseQuery 处理程序中实现它,但这意味着逻辑在表单控件表单本身中都是重复的。(表单控件用在其他地方,我想避免改变它的行为)。

(我推测 CMExit 没有被触发,因为控件实际上从未失去焦点。表单已关闭,但表单控件仍然在关闭的表单上“具有焦点”。)

4

1 回答 1

7

关闭窗体不一定会触发 TControl 的退出事件。例如,用户可以按 Alt-F4。

我建议将验证移动到一个单独的过程中,并从退出关闭事件中调用该单独的过程。

以下代码无需太多修改即可工作:

function TDBCalendar.UpdateSuccessful: boolean;
begin
  { tell data link to update database }
  { if successful, return True, else return False }
  { this function must be Public if a form is gonna check this value }
  Result := True;
  try
    FDataLink.UpdateRecord;
  except on Exception do
    Result := False;
  end;
  inherited;
end;

procedure TDBCalendar.CMExit(var Message: TWMNoParams);
begin
  //if not valid, then don't let them leave
  if not(UpdateSuccessful) then begin
    SetFocus;
  end;
end;

///////////////////////////////////////////
//on the form that contains your control...
///////////////////////////////////////////

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //if not valid, then don't let them close the form
  if not(dbcal.ControlIsValid) then begin
    Action := caNone;
  end
  else begin
    inherited;
  end;
end;
于 2009-04-06T17:39:37.803 回答