Lazarus

Programming => General => Topic started by: Marion on July 29, 2021, 09:47:41 pm

Title: How to keep track of child forms
Post by: Marion 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?
Title: Re: How to keep track of child forms
Post by: winni 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

Title: Re: How to keep track of child forms
Post by: Marion 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;
Title: Re: How to keep track of child forms
Post by: jamie 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.
Title: Re: How to keep track of child forms
Post by: Remy Lebeau 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.
TinyPortal © 2005-2018