Board index » cppbuilder » Getting an MDI child's parent form?

Getting an MDI child's parent form?

How can an MDI child get its parent form?  When I create a new MDI child
form, I do it like this:

TMyMDIChild *pForm = new TMyMDIChild( Application );

The child form's PArent property is NULL, so I thought maybe I needed to set
the Parent property of the child form:

pForm->Parent = this;  // "this" is the MDI parent form

But if I do this, then the application crashes when the child form closes
(via caFree in the Close event).

So, if the Parent property cannot be set, I suppose I could use the Tag
property, but that seems like a "hack."  What is the usual way for an MDI
child to get its parent form?

- Dennis

 

Re:Getting an MDI child's parent form?


Only one form at a time in the project can be an MDI parent.  There are no
functions or properties that return the current MDI parent in a single call,
but you can iterate through the Screen->Forms collection looking for a form
that has it's FormStyle propety set to fsMDIForm:

    TForm* __fastcall GetMDIParent()
    {
        TForm *MDIParent = NULL;
        for(int x = 0; x < Screen->FormCount; x++)
        {
            MDIParent = Screen->Forms[x];
            if(MDIParent->FormStyle == fsMDIForm)
                return MDIParent;
        }
        return NULL;
    }

Gambit

Quote
"Dennis Jones" <djo...@oregon.com> wrote in message

news:3c091f5b$1_2@dnews...
Quote
> How can an MDI child get its parent form?

Re:Getting an MDI child's parent form?


Thanks.  I decided to simply use "Application->MainForm" since it is my MDI
parent and is accessible by all the child windows.

- Dennis

Quote
"Remy Lebeau" <gambi...@gte.net> wrote in message news:3c095821$3_1@dnews...
> Only one form at a time in the project can be an MDI parent.  There are no
> functions or properties that return the current MDI parent in a single
call,
> but you can iterate through the Screen->Forms collection looking for a
form
> that has it's FormStyle propety set to fsMDIForm:

>     TForm* __fastcall GetMDIParent()
>     {
>         TForm *MDIParent = NULL;
>         for(int x = 0; x < Screen->FormCount; x++)
>         {
>             MDIParent = Screen->Forms[x];
>             if(MDIParent->FormStyle == fsMDIForm)
>                 return MDIParent;
>         }
>         return NULL;
>     }

> Gambit

> "Dennis Jones" <djo...@oregon.com> wrote in message
> news:3c091f5b$1_2@dnews...
> > How can an MDI child get its parent form?

Re:Getting an MDI child's parent form?


Quote
Dennis Jones <djo...@oregon.com> wrote in message news:3c091f5b$1_2@dnews...
> How can an MDI child get its parent form?  When I create a new MDI child
> form, I do it like this:

> TMyMDIChild *pForm = new TMyMDIChild( Application );

Might sound stupid, but why don't you make the MDI frame form the owner of
the MDI child? In terms of direct dependencies it seems more logical to me
that the parent deletes all of it's childs before the application deletes
the main form at the end.

TMyMDIChild *pForm = new TMyMDIChild( MDIFrameForm);

BTW this is the code generated by BCC IDE when using the repository:

void __fastcall TMainForm::CreateMDIChild()
{
  TMDIChild *Child;
  //--- create a new MDI child window ----
  // Parse the file name & properties by the Tag property
  Child = new TMDIChild(this);
  Assert(Child);

Quote
}

Alex

Other Threads