Difference between Owner and Parent??

Posted by dragon on Sunday, 9 October 2011

type="html">Whenever we try add a button on a panel in a form, we try to make an invisible connection between the components. As we add the button on the panel, the panel automatically is set as the Parent of the button while the form becomes the owner.

Owner property is a read only property and cannot be modified, however parent for a control can be set easily using the following code:

button1.parent := form1; // this could be to any control

If we try to create a new component say at runtime, then we need to set the parent property of the control to make it visible.

The Owner component basically takes the responsibility of freeing the allocated memory of the owned components. The Owner is being set while invoking the create constructor and becomes a read-only property so can't be changed later on.

The Parent component on the other end determines how the child (i.e. contained component is to be displayed). All the properties like left, top etc are relative to the parent component.


(Note: Form is the owner of all the components residing inside it. So whenever the form is displayed then all the components memory is freed automatically.)

To understand the concepts lets develop a test code...
So, open delphi and open a form. Place a button on the form say button1. Place two edit text on the form say edit1 and edit2.

Double click on button1 and write the following code (View the sample code below):

procedure TForm1.Button1Click(Sender: TObject);
var
myform : tform;
begin
edit1.text := '';
edit2.text := '';
myform := tform.create(self);
myform.parent := button1;
myform.visible := true;
myform.caption := 'MyForm';
Edit1.Text := 'Owner: ' + myform.owner.name;
edit2.text := 'Parent: ' + myform.parent.name;
end;

Run the program and you will see that as you click on the button1. A form is displayed inside the button1 control... :)... Nice.. Hmmm.. Well all this happened because we have set the parent of our form (i.e. myform as button).

Well now you may ask what if I don't set the parent property. Well the answer is simple your form won't get displayed as delphi doesn't knows which form/control should display our form.

Now, you say what if I would have set parent of myform as form1 rather than button1. The answer is that our form i.e. myform will be displayed withing form1 now and will be visible withing the boundaries of form1... :)..

Now what if I say that we live in a free world and our newly created form has been bounded within form1. What should I do. Hmm now that pretty simple. Simply set the parent property of our form to nil. This will let our form be created outside the boundaries of form1. The code for this will be:

procedure TForm1.Button1Click(Sender: TObject);
var
myform : tform;
begin
edit1.text := '';
edit2.text := '';
myform := tform.create(nil);
myform.parent := nil;
myform.visible := true;
myform.caption := 'MyForm';
try
Edit1.Text := 'Owner: ' + myform.owner.name;
Edit2.text := 'Parent: ' + myform.parent.name
except
end;
end;


{ 0 comments... read them below or add one }

Post a Comment