Board index » cppbuilder » Want To Create, Manage Multiple Instances Of A Child Form

Want To Create, Manage Multiple Instances Of A Child Form

I have a class called TQVGraphForm that I want to create multiple instances of
from another form. I have several problems I need to solve and am looking for
tips:

1) The parent form is going to do C++ "new" calls to construct the children
since the user can make more than one instance of the same child form class.
   Does the parent do deletes at the end? If the user closes a form himself
then does that cause the OS to do the delete of that form object?

2) How to have the parent know that a form has been destroyed? Would I notify
the parent with some sort of callback method on the OnClose or of the OnDestroy
of the child form? Which method in the child to use?
   I figure the parent could remove the child from the parent's list of
children it is maintaining for this purpose. But I assume in the scenario
where the user clicks on the form to close it that the parent should not do a
delete call on the child object pointer.

3) The parent form has to keep track of which child form last had focus. The
reason is that the last one that had focus is the default target to be given
new pieces of data from the parent form.
   How best to manage the call-backs to the parent form? I think (correct me if
I'm wrong) I want to use the OnActivate event to detect in the child forms when
each gets focus. Somehow I need to call back to the parent to tell it teach
time when a different child has focus.

 

Re:Want To Create, Manage Multiple Instances Of A Child Form


Quote
"Randall Parker" <rgpar...@west.net> wrote in message

news:MPG.1945aa754137bdd798979a@forums.inprise.com...

Quote
>    Does the parent do deletes at the end?

Not automtically, no.  You have to do that yourself.  Unless you assign the
parent form as the Owner of the child forms, in which case the child forms
will be automatically freed when the parent form itself is freed.

Quote
> If the user closes a form himself then does that
> cause the OS to do the delete of that form object?

The OS does nothing.  The VCL, on the other hand, does nothing be default,
but you can assign an OnClose event handler to the form and set the Action
parameter to caFree.  Then the form will free its own memory when it is
closed.

Quote
> 2) How to have the parent know that a form has been destroyed?

Dynamically assign an OnClose event handler to the child form when you
create an instance of it, and have the event handler be a member of the
parent form instead of the child form.  By being a member of the parent
form, the event handler will have access to the parent's data, and can
perform whatever updating is needed.

Quote
> 3) The parent form has to keep track of which child form last had focus.

Use the child form's OnActive and OnDeactive events to inform the parent
form of focus changes.

Quote
> Somehow I need to call back to the parent to tell
> it teach time when a different child has focus.

Again, make the event handlers member of the parent form rather than the
child form, and then assign them dynamically at runtime when you create new
instances of the child form.

BTW, just curious, is your parent form actually the project's main form?  If
so, are your child forms required to be free-floating forms or will having
them reside physically inside the parent form be ok for you?  If so, then
you should consider using MDI.  It already handles all of these details for
you.  You don't need to track closures or focus changes yourself at all.  In
the case of sending data to the active child form, simply use the parent
form's ActiveMDIChild property to know which child is currently in focus at
any given time.

Gambit

Other Threads