Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Form creation and destruction - instances of a form


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Apr 13, 2017 12:19 PM Last Post By: Remy Lebeau (Te...
Toby Dobbs

Posts: 77
Registered: 9/24/16
Form creation and destruction - instances of a form  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 12, 2017 5:42 AM
Hi,

I have a form which spawns another form according to the following:

settingsWindow: TFormChartSettings;
...
if settingsWindow = Nil then
begin
  Application.CreateForm(TFormChartSettings, settingsWindow);
  settingsWindow.Init(MainChart);
end;


TFormChartSettings has a method

procedure TFormChartSettings.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  FormChartSettings := Nil;
  Action := caFree;
end;


The method above does not handle the object but rather the class does there exist a way of doing this. If so, how?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Form creation and destruction - instances of a form  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 12, 2017 9:36 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby wrote:

I have a form which spawns another form according to the following:

settingsWindow: TFormChartSettings;
...
if settingsWindow = Nil then
begin
Application.CreateForm(TFormChartSettings, settingsWindow);
settingsWindow.Init(MainChart);
end;

Although that will certainly work, I would suggest instead defining a constructor
that accepts a MainChart as input, and then call that constructor directly
instead of using Application.CreateForm() at all:

type
  TFormChartSettings = class(TForm)
    ...
  public
    constructor CreateWithInit(Chart: TMainChart);
    ...
  end;
 
constructor TFormChartSettings.CreateWithInit(Chart: TMainChart);
begin
  inherited Create(Application);
  // init as needed...
end;
 
...
 
if settingsWindow = Nil then
begin
  settingsWindow := TFormChartSettings.CreateWithInit(MainChart);
end;


TFormChartSettings has a method
<snip>
The method above does not handle the object but rather the class does
there exist a way of doing this. If so, how?

That OnClose handler is resetting the FormChartSettings variable (BTW,
you should be using the OnDestroy event instead to reset the variable), but
your code that is creating the Form object is using the settingsWindow
variable instead. You need to get rid of the settingsWindow variable and
use the FormChartSettings variable instead:

if FormChartSettings = Nil then
begin
  Application.CreateForm(TFormChartSettings, FormChartSettings);
  FormChartSettings.Init(MainChart);
  // or:
  // FormChartSettings := TFormChartSettings.CreateWithInit(MainChart);
end;


--
Remy Lebeau (TeamB)

---
This email has been checked for viruses by AVG.
http://www.avg.com

Toby Dobbs

Posts: 77
Registered: 9/24/16
Re: Form creation and destruction - instances of a form  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 12, 2017 11:29 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks, this helps a lot but what if I wanted to have multiple instances of the form running simultaneously?
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Form creation and destruction - instances of a form  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2017 12:50 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:

Thanks, this helps a lot but what if I wanted to have multiple
instances of the form running simultaneously?

Then your code could be something like this:

{code}
procedure TFormChartSettings.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if self=settingsWindow then
settingsWindow :=nil;
if self=FormChartSettings then
FormChartSettings := Nil;

Action := caFree;
end;

When you have a global reference to an instance you have to make sure
to manage that correctly. When you free an instance you have to remove
the global reference to it or face an Access Violation error later.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Form creation and destruction - instances of a form  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 13, 2017 12:18 PM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby wrote:

what if I wanted to have multiple instances of the form
running simultaneously?

Then you will have to remove the "if FormChartSettings = Nil" check, and
in fact ignore the global FormChartSettings pointer altogether. A pointer
can only point at one time at a time. Use a local pointer variable when
creating the Form instances. You can use the TScreen.Forms[] list, or a
T(Object)List, to keep track of your form instances, if needed. You can
use the OnClose (better, OnDestroy) event to remove the Sender from a
T(Object)List (the TScreen.Forms[] list is managed automatically by the VCL).

--
Remy Lebeau (TeamB)


---
This email has been checked for viruses by AVG.
http://www.avg.com

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02