Link to home
Start Free TrialLog in
Avatar of Trevor Mifsud
Trevor Mifsud

asked on

Multiple MDI Child Forms!! HELP!

Hi Experts,

I have developed a delphi vcl application using Borland Development Studio 2006. For the first time in a long time im using MDI and MDI Child forms but seem to be having a problem.

When i load the MDI Child window into the MDI form it all works fine, but if i click the button again i get a second instance of the form.

What i want is only one instance of the form to load and by any chance the form is minimised i would like when the button is pressed a again, the form will restore from its minimised state.

Thank you in advance.




Trevor
ASKER CERTIFIED SOLUTION
Avatar of shiyunchi
shiyunchi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Trevor Mifsud
Trevor Mifsud

ASKER

hi shiyunchi,

Im getting an error on this line:

Form := Screen.Forms[i];

its saying:

Incompatible Types TForm2 and TForm

Is it because TForm2 is a child window?


Trevor
Avatar of dinilud
if assigned(Form2) then
begin
   Form2.WindowsState := wsNormal;
end
else
begin
  Form2:=TForm2.Create(self);
end;
Form2.Show;
I use this and it work fine to me: I have method CheckIfChildExist with parameter n - this is name of Child form - not class name but name of child form. On this way, you can create multiple child forms of same class but with different names, and control them to have only one instance.

function TFMain.CheckIfChildExist(n: string): boolean;
var
  i: integer;
begin
  Result := False;
  for I := 0 to MDIChildCount - 1 do    { Iterate }
  begin
    if (MDIChildren[i].Name = n) then
    begin
      MDIChildren[i].BringToFront;
      if MDIChildren[i].WindowState = wsMinimized then
        MDIChildren[i].WindowState := wsNormal;
      Result := True;
      exit;
    end;
  end;    { for }
end;

Then, when I want to create/show child form, I use this...

procedure TFMain.actChildForm1Execute(Sender: TObject);
var
  ChildForm1: TFChildForm1;
begin
  if CheckIfChildExist('ChildForm1 - something') then
    exit; // you don't have to create again
  ChildForm1:= TFChildForm1.Create(self);
  ChildForm1.Name:='ChildForm1 - something';
end;

... and, for example, another instance of same class,...

procedure TFMain.actChildForm2Execute(Sender: TObject);
var
  ChildForm2: TFChildForm1;
begin
  if CheckIfChildExist('ChildForm2 - something else') then
    exit; // you don't have to create again
  ChildForm2:= TFChildForm1.Create(self);
  ChildForm2.Name:='ChildForm2 - something else';
end;

TFChildForm1 have caFree assigned as close action in OnClose Event.
You might find it easier just to track the single instance of the child from the main form.
For example, create a variable thisChildForm: TForm on the main form.
It should be nil on startup.
Then when you click the button on your main form, keep a pointer to it here.
Then all you need to do next time is to check if assigned( thisChildForm) then exit
This will then not create a second copy next time your button is clicked.
Pretty simple way if you are absolutely sure you only want a single occurrence of the form.