Solved

Delphi Form ownership

Posted on 2016-09-03
4
89 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 26

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

777 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