Mirror

How to create only one instance of a MDI child form (3) (Views: 705)


Problem/Question/Abstract:

How can I prevent to open a MDIChild if it is already open? When I select the same option on the menu, the first code executes again and I get two forms.

Answer:

procedure TformMain.doDisplayCustomerLookupGrid(Sender: TObject);
var
  MyChildFormName: string;
  MyChild: TformCustGrid;
  I: Integer;
begin
  MyChildFormName := 'Customer Lookup';
  { If the child form already exists, make it active }
  with formMain do
    for I := 0 to MDIChildCount - 1 do
    begin
      if MDIChildren[I].Caption = MyChildFormName then
      begin
        MDIChildren[I].BringToFront;
        exit;
      end;
    end;
  { If the child form does not exist, create it }
  MyChild := TformCustGrid.Create(Application);
  MyChild.Caption := MyChildFormName;
end;

<< Back to main page