Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who owns who.. 1

Status
Not open for further replies.

Oppenhiemer

Programmer
Jun 28, 2001
315
GB
Hi -

I have a problem with dynamic form creation which I think probably relates to ownership issues.

This is the situation, I have 3 forms - the first form is the only one created automatically when the program starts. The other two forms are created using ..
===========================================
Var
POptions : TProgOptions;

begin
POptions := TProgOptions.Create(Nil);
if POptions.showmodal = mrOk then
;
POptions.Free;
===========================================

That sort of code. The problem occurs as follows..
1. Form 1 creates and shows form 2
2. Form 2 creates and shows form 3
3. Form 3 gets a value from the user and tries to update a field on Form 2.
4. This is where an exception occurs. Actually an "Access Violation" error.

Im guessing that its a problem of ownership of some sort. Maybe I need to specify the Parent property for the synamically created forms ? Anyway, any help would be much appreciated..

Opp.



 
Where is the POptions variable declared? That'll be your problem.
 
hi Opp

Parent and Owner are two different things.

In form 2 you could have

form3 := myform3.create(self);//self being form2

later in form 3

(Owner as Tform2).myfield := ...

Is this helpful ?

lou
 
FYI, took this from the help:

Note: Don't confuse the Parent property declared in TControl with the Owner property declared in TComponent. The Parent of a control is always a windowed control that visually contains the control, and which is responsible for writing the control to a stream when the form is saved. The Owner of a component is the component that was passed as a parameter in the constructor and that controls when the component is freed.
 
I usually go
Code:
POptions := TProgOptions.Create(Application);
, as I think this means that when the application is destroyed, it will free my form; assuming that I have been so gauche as to fail to grant it freedom myself.

And use
Code:
POptions.Release
instead of
Code:
.Free
. Quoth the help:
Release does not destroy the form until all event handlers of the form and event handlers of components on the form have finished executing.

Now as to your actual question:
Form1 can access public members of Form2, as it has a nice variable by which to refer to it; properly instantiated, allocated, and such like with a Create(). If you go
Code:
Var POptions : TProgOptions;
and then refer to the new form right away, Access will duly be violated; it's only after the Create() that our variable points to something useful, and indeed useable. Form2 can access Form3 for a similar reason, but Form3 has no such mechanism whereby it can reach within the guts of Form2.

You have to tell Form3 about Form2 by passing it a variable of type TForm2, containing TForm2 within it. (Don't worry, the compiler will actually pass by reference behind the scene, so we're not throwing massive data structures around.)

Luckily, you can do this in the way that 'weez suggested above; with the
Code:
Create(self
), and the
Code:
(Owner as TForm2).mycontrol
. Of course, you'll have to name Form2 in Form3's uses clause, so that it knows what controls exist within Form2. -- Doug Burbidge mailto:doug@ultrazone.com
 
Hi -

Well mikeEd I made the form variable in question global - but that made no difference.

I also tried what you mentioned Lou and cast the reference to the field to be updated - but that only generated an adbstract error and then the same exception as before.

Here is a brief overview of the current problem ..


All the forms in question (with the exception of form1) are being created using the statement ..

F := Formx.Create(self);
if F.showmodal = mrOk then
F.Free;

Where x varies depending on the form in question. I have also tried Create(nil) as an option - but with no luck.

Anyway, thanks for the feedback - any further help - much appreciated ..

Cheers..

Opp.
 
If Form3 wants to update a value on form2, form2_unit must be declared in the use section of Form3.

Maybe you have a circular link problem

a workaround is a global variable in a unit which can be accessed by all forms or

if F.showmodal = mrOk then
begin
YourUpdate etc..
F.Free;
end;

Maybe later I send a better example when I am behind my coding machine

S. van Els
SAvanEls@cq-link.sr
 
Doh!

I tried the method that weez suggested (and that ronin441 pointed me back to) and it works!

The reason that it failed the first time was due to the way I implemented it. So thanks again to Weez and Ronin441.

Cheers all..

Opp.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top