[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multiple MDI Child Forms!! HELP!

Posted on 2007-08-12
6
Medium Priority
?
417 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:trevsoft
6 Comments
 

Accepted Solution

by:
shiyunchi earned 1000 total points
ID: 19679443
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
 
LVL 1

Author Comment

by:trevsoft
ID: 19680450
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
 
LVL 10

Expert Comment

by:dinilud
ID: 19681426
if assigned(Form2) then
begin
   Form2.WindowsState := wsNormal;
end
else
begin
  Form2:=TForm2.Create(self);
end;
Form2.Show;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:schild1
ID: 19681963
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 19683045
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question