Board index » delphi » No multiple Child windows in MDI application ?

No multiple Child windows in MDI application ?

I want in my MDI application, to have only ONE instance of child window of
particular kind. How can I test for presence of that window, so I can
preserve from making multiple windows of that kind  which I don't need ?
TIA
Slobodan
 

Re:No multiple Child windows in MDI application ?


On Tue, 18 Jan 2000 18:05:49 +0100, "Beric Slobodan"

Quote
<coba...@skloniovo.ptt.yu> wrote:
>I want in my MDI application, to have only ONE instance of child window of
>particular kind. How can I test for presence of that window, so I can
>preserve from making multiple windows of that kind  which I don't need ?

I define the variable ahead of and verify it's nil before initiation.
you need to set the variable to nil in the OnDestroy method. so,

var MyForm : TChildForm;

procedure TChildForm.OnDestroy;
begin
  MyForm := nil;
end;

*******************
uses ChildForm;
procedure TParentForm.Whatever;
begin
  if MyForm = nil then MyForm := TChildForm.Create (self) else
    MyForm.BringToFront;
end;

Re:No multiple Child windows in MDI application ?


Remember that, in an MDI application that is going to support multiple
instances of the same child-form, Delphi's standard behavior of creating
a single global "var" pointing to the form-object, and its standard
behavior of creating all the forms at startup time, will not work.
Instead, all of the form-instances will simply reside somewhere on the
list of forms maintained by the "Application" object, and you must
locate the instance in that list when you need it.

Here are a couple of routines from an application we wrote, which as it
happens does not allow more than one instance of a form-class to exist
(this by choice), but which does illustrate the type of work that is
involved.  This app used resource-strings for all messages, and has some
utility-routine calls that are specific to our work, but you can see the
essential idea.

<snip!>

{
  Baby-making.  :-)
  The main form is an MDI-child application.  The "MakeAnotherChild"
procedure
    reproduces indiscriminately (ahem...), while "ActivateOrMakeChild"
searches
    to see if a child-window of this type exists.  If it does, the
existing child is
    brought front-and-center.
  By design, multiple instances of child-forms are never allowed to
exist.
    (We'd have to have multiple instances of the data-model, for one
thing,
    and we'd confuse the hell out of users, for another...)

Quote
}

function TMainForm.MakeAnotherChild(ofClass: TFormClass) : TForm;
var
  f:  TForm;
begin
  Application.CreateForm(ofClass, f);
  if f.FormStyle <> fsMDIChild then begin   { common mistake }
    SayStop(FmtLoadStr(M_FormNotMDIChild, [f.ClassName]));
  end;
  result := f;
end; {MakeAnotherChild}

function TMainForm.ActivateOrMakeChild(ofClass: TFormClass) : TForm;
var
  n:  integer;
  f:  TForm;
begin
  { Search for the most recently created form of the specified class }
  f := nil;
  for n := MDIChildCount - 1 downto 0 do begin
    if MDIChildren[n] is ofClass then begin
      f := MDIChildren[n];     { found it - note and break out of loop }
      break;
    end;
  end;
  if Assigned(f) then begin            { found it? }
    F.BringToFront;                    { make it come to the front }
    if F.WindowState = wsMinimized
      then F.WindowState := wsNormal;  { and be sure it's large enough
we can see it }
  end else begin
    Application.CreateForm(ofClass, f);
    if f.FormStyle <> fsMDIChild then begin   { common mistake }
      SayStop(FmtLoadStr(M_FormNotMDIChild, [f.ClassName]));
    end;
  end;
  result := f;
end; {ActivateOrMakeChild}
</snip!>

Quote
>Beric Slobodan wrote:

> <jlawre...@pitt.edu> wrote
> >   if MyForm = nil then MyForm := TChildForm.Create (self) else

> Thanks a lot !
> Some people told me to do this checking with function Assigned, but this
> don't work :
> if not Assigned(ChildForm) then ...

--------------------------------------------------------------------
Sundial Services :: Scottsdale, AZ (USA) :: (480) 946-8259
mailto:i...@sundialservices.com  (PGP public key available.)
Quote
> Why =shouldn't= it be quick and easy to keep your database online?
> ChimneySweep(R):  "Click click, it's fixed!" {tm}
> http://www.sundialservices.com/cs3web.htm

Re:No multiple Child windows in MDI application ?


<jlawre...@pitt.edu> wrote

Quote
>   if MyForm = nil then MyForm := TChildForm.Create (self) else

Thanks a lot !
Some people told me to do this checking with function Assigned, but this
don't work :
if not Assigned(ChildForm) then ...
Do you know why ?

regs
Coba

Other Threads