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
LVL 1
trevsoftAsked:
Who is Participating?
 
shiyunchiCommented:
Suppose that the FormStyles of TMainForm and TChildForm are fsMDIForm and fsMDIChild, respestively.
When the button is clicked, one instance of TChildForm is created.

var
  I: Integer;
  Found: Boolean;
  Form: TForm;
begin
  Found := False;
  Form := nil;
  for I := 0 to Screen.FormCount - 1 do
  begin
    Form := Screen.Forms[I];
    Found := Form.ClassNameIs('TChildForm');
    if Found then Break;
  end;
  if Found then
  begin
    Form.WindowsState := wsNormal;
  end
  else begin
    Form := TChildForm.Create(Application);
  end;
end;
0
 
trevsoftAuthor Commented:
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
0
 
diniludCommented:
if assigned(Form2) then
begin
   Form2.WindowsState := wsNormal;
end
else
begin
  Form2:=TForm2.Create(self);
end;
Form2.Show;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
schild1Commented:
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.
0
 
mikelittlewoodCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.