Solved

Delphi Form ownership

Posted on 2016-09-03
4
123 Views
Last Modified: 2016-09-22
I want to create multiple instances of a (sub)Tform  that all are owned by another (main)Tform from where they where created. If the user closes a (sub)form, it should be destroyed and not be owned by the mainform. If the Mainform is closed, all subforms should all be destroyed. I guess i can do this with a new TComonentlist, but is there any functionality in the form that i can use? (probably also a TComonentlist) ?
0
Comment
Question by:Sunsales
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 41783248
Delphi's Ownership handle such things....
So... create forms under Main Form ownership .... and on sub form destroy - Main form will be notified... and sub form will be excluded from component list...
//main form
procedure TfrmMain.StartSubForm();
var
  childform: TSubForm;
begin
   childform := TSubForm.Create(frmMain);
   childform.Show; //show and do not block main form
end;

Open in new window


Child form's OnClose event:
procedure TSubForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Self.Hide;
  Action:=caFree;  //sub form want to be free.....
end;

Open in new window

1
 

Author Comment

by:Sunsales
ID: 41783301
Can I use a TobjectList instead of the Tform as owner and still get an automatic delete in the TobjectList  when i free the form?
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 41783587
there is no point in maintaining your own list
all components create with an owner are stored inside the parent container component
delphi does by default what you ask

except ... when you create components/forms with nil as owner
SubForm := TSubForm.Create(nil);

an object instance which is freed doens't have an owner
it just doesn't exist anymore

mind the difference between closing/hiding and free a form
1
 

Author Closing Comment

by:Sunsales
ID: 41811427
Thanx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question