MDI 应用程序,检查是否打开了具有相同标题的 child 表单

MDI Application, check if a child form with the same caption is open

我有一个 Delphi MDI 应用程序,它有一个客户搜索 child 表单,只能打开一次(检查 isAssigned),但是查看/编辑表单可以打开多次,以便最终用户可以一次打开多个客户(选项卡式),我想做的是能够阻止他们多次打开同一个客户记录,在打开客户表格时我将标题设置为客户帐户参考,如果该表格存在,我想 .BringToFront,如果没有,我会创建它。

请问最好的方法是什么,因为我正在摸不着头脑!

提前致谢。

procedure TfrmCustomerSearch.ViewCustomerExecute(Sender: TObject);
begin
  screen.cursor := crappstart;

  if not IsMDIChildOpen(frmMainMenu, 'frmCustomerView', pfrmCaption) then
    frmCustomerView := TfrmCustomerView.createform(nil,dmCustomerSearchfrm.FDQCustSearchreference.Value,cxGrid1DBTableView1.DataController.FocusedRecordIndex)
  else
    frmCustomerView.BringToFront;

  screen.cursor := crdefault;
end;

function TfrmCustomerSearch.IsMDIChildOpen(const AFormName: TForm; const AMDIChildName, AMDICaption : string): Boolean;
var
  i: Integer;
begin
  Result := False;
  for i := Pred(AFormName.MDIChildCount) DownTo 0 do
    if (AFormName.MDIChildren[i].name = AMDIChildName) then
    begin
      if (AFormName.MDIChildren[i].caption = AMDICaption) then
        begin
          Result := True;
          Break;
        end

    end;
end;

尝试更像这样的东西:

procedure TfrmCustomerSearch.ViewCustomerExecute(Sender: TObject);
begin
  Screen.Cursor := crAppStart;
  try
    frmCustomerView := TfrmCustomerView(FindMDIChildOpen(frmMainMenu, TfrmCustomerView, pfrmCaption));
    if frmCustomerView = nil then
      frmCustomerView := TfrmCustomerView.CreateForm(nil, dmCustomerSearchfrm.FDQCustSearchreference.Value, cxGrid1DBTableView1.DataController.FocusedRecordIndex);
    frmCustomerView.BringToFront;
  finally
    Screen.Cursor := crDefault;
  end;
end;

function TfrmCustomerSearch.FindMDIChildOpen(const AParentForm: TForm; const AMDIChildClass: TFormClass; const AMDICaption : string): TForm;
var
  i: Integer;
  Child: TForm;
begin
  Result := nil;
  for i := Pred(AParentForm.MDIChildCount) DownTo 0 do
  begin
    Child := AParentForm.MDIChildren[i];
    if Child.InheritsFrom(AMDIChildClass) and
       (Child.Caption = AMDICaption) then
    begin
      Result := Child;
      Exit;
    end;
  end;
end;