Solved

Delphi Form ownership

Posted on 2016-09-03
4
50 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 25

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 36

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now