Recent

Author Topic: How to keep track of child forms  (Read 2191 times)

Marion

  • Jr. Member
  • **
  • Posts: 93
How to keep track of child forms
« on: July 29, 2021, 09:47:41 pm »
I have a project that has a main form, and can open any number of child forms. If someone wants to reopen an object that already has an open form I want to bring it to the front, otherwise, open a new form for that object.

I have tried keeping a list of the forms but because the list exists the closed forms never gets freed. What is the best way to keep track of the child forms?
Thank you,
Marion
(A recovering Windows programmer.)

winni

  • Hero Member
  • *****
  • Posts: 2668
Re: How to keep track of child forms
« Reply #1 on: July 29, 2021, 10:13:23 pm »
Hi!

You dont need to keep track of your child forms if you dont create them dynamic.

If you have a lot of child forms they are shown with FormX.show (and NOT with FormX.ShowModal).

Form.Show behaves this way:

* If it is not active it is activated and brought to front
* If it is active but hidden by other Forms (or other apps) it is brought to Front.

That is the whole secret.

Winni


Marion

  • Jr. Member
  • **
  • Posts: 93
Re: How to keep track of child forms
« Reply #2 on: July 29, 2021, 11:45:14 pm »
This is what I have done.

When I create a form I make the parent form the owner:

Code: Pascal  [Select][+][-]
  1. newform := TnewForm.Create(parentform);

In the OnClose event of the form I set:

Code: Pascal  [Select][+][-]
  1. CloseAction := caFree;

When I need to find a form I use the parent form's Components list and check for the type of form I want, when I find it I then check to see if it is the form with the right object.

Code: Pascal  [Select][+][-]
  1. for i := 0 to parentform.ComponentCount - 1 do
  2.   if parentform.Components[i] is TnewForm then
  3.     if TnewForm(parentform.Components[i]).Value = myvalue then
  4.       TnewForm(parentform.Components[i]).BringToFront;
Thank you,
Marion
(A recovering Windows programmer.)

jamie

  • Hero Member
  • *****
  • Posts: 4846
Re: How to keep track of child forms
« Reply #3 on: July 30, 2021, 12:22:21 am »
You could always look at the TScreen or SCREEN class, that has most if not all the info about forms, monitors etc  in there.
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 990
    • Lebeau Software
Re: How to keep track of child forms
« Reply #4 on: July 30, 2021, 05:20:08 pm »
If someone wants to reopen an object that already has an open form I want to bring it to the front, otherwise, open a new form for that object.

I usually just loop through the TScreen.Forms list looking if the desired Form already exists, if so then Show() it, otherwise create a new instance.

I have tried keeping a list of the forms but because the list exists the closed forms never gets freed.

Why would that be?  Objects are not reference counted, so just keeping a list of object pointers would not prevent those objects from being destroyed.  Closing a Form does not auto-destroy it, though.  You need to set CloseAction=caFree in the Form's OnClose event if you want that behavior.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018