Board index » delphi » MDI Child Form Creation Question

MDI Child Form Creation Question

Perhaps I'm just displaying my ignorance here, but...

I've got an MDI application that works just fine, except, because it's a
database front end, I don't want to have more than one instance of the
same child form.  The forms I am using are not Auto-created, so i'm
using the

with TForm.Create(MDIFrame) do show;

way of creating forms.  But I want to determine whether or not instances
of those forms already exist as active MDI Children, and if so, just
show it, rather than create a new instance.  Is there anyway to tell if
a given form is open?

Anybody want to help me on this one?  Thanks in Advance.

Sean Upton
Correct Technologies

 

Re:MDI Child Form Creation Question


Quote
Sean D. Upton wrote:

[snip]
Quote
> database front end, I don't want to have more than one instance of the
> same child form.  The forms I am using are not Auto-created, so i'm
> using the

[snip]

Don't know if this is the best way, because I'm still learnig Delphi
2.01, but it works for me...

function TMyApp.CheckForForm(cFormName: String): Boolean;
{
 Routine which returns True if a form by the Name of cFormName has been
created, and
 return False if it has not been.

Quote
}

var
  I: Integer;
begin
  with ColmanApp do
    begin
    result := False;
    for I := MDIChildCount-1 downto 0 do
      if MDIChildren[I].Name = cFormName
      then result := True
    end;
end;

I have also seen postings that indicate you can check for
ASSIGNED(FormVar) to see if a form has been created.  More efficient to
do it this way I image.  The only catch is, if you free the form
remember to assign NIL to the form variable or the next time assigned()
will return TRUE.

Then on the menu OnClick() event that creates the form I have this
routine:

procedure TMyApp.Company1Click(Sender: TObject);
begin
  if CheckForForm('CompanyForm')
  then
    begin
      CompanyForm.BringToFront;
    end
  else
    begin
      Application.CreateForm(TCompanyForm,CompanyForm);
      CompanyForm.Show;
    end;
end;

Hope this helps...

Re:MDI Child Form Creation Question


Quote
Larry Hengen <henge...@cadvision.com> wrote:
>Sean D. Upton wrote:
>[snip]
>> database front end, I don't want to have more than one instance of the
>> same child form.  The forms I am using are not Auto-created, so i'm
>> using the
>[snips]
>I have also seen postings that indicate you can check for
>ASSIGNED(FormVar) to see if a form has been created.  More efficient to
>do it this way I image.  The only catch is, if you free the form
>remember to assign NIL to the form variable or the next time assigned()
>will return TRUE.

To my experience the last statement is a bit dubious. If you call
  myForm.free;
- as far as I have found - it doesnt mean the form is out of memory
at once. So if you assign NIL to the form pointer, this could mean you
get memory leaks. There is a property called ComponentState, which can
be used to check for working forms like this:
  if (myForm<>nil) and (myForm.componentState=[]) then
     { ok to work with myForm }

If Ive mixed things up - would someone please point it out to me?

Anders

Re:MDI Child Form Creation Question


socra...@shell.aros.net (Sean D. Upton)  wrote

Quote
> way of creating forms.  But I want to determine whether or not instances
> of those forms already exist as active MDI Children, and if so, just
> show it, rather than create a new instance.  Is there anyway to tell if
> a given form is open?

Sean, try this code:

for i := 0 to MDIChildCount - 1 do
  if MDIChildren[i] is TTheChildTypeYouWantToTestFor then
    MDIChildren[i].Show

Best Regards

Michael
--

Michael Suedkamp                                      m...@suedkm.franken.de

Re:MDI Child Form Creation Question


Quote
> way of creating forms.  But I want to determine whether or not instances
> of those forms already exist as active MDI Children, and if so, just
> show it, rather than create a new instance.  Is there anyway to tell if
> a given form is open?

If you dynamically create a form, the Name property will be Nil. So set it
to something, like the class name, and then iterate through the MDIChildren
property of the MDI parent checking each form's Name for a match.

--

Mark P
-----------
http://www.ozemail.com.au/~pritchma

Other Threads