1. Computing

Owner vs. Parent in Delphi Applications

By

Owner vs. Owner in Delphi

Owner vs. Owner in Delphi

Every time you place a panel on a form and a button on that panel you make an "invisible" connection! The Form becomes the owner of the Button, and the Panel is set to be it's parent.

Every Delphi component has an Owner property. Owner takes care of freeing the owned components when it is being freed.

Similar, but different, the Parent property indicates the component that contains the "child" component.

Parent

Parent refers to the component that another component is contained in, such as TForm, TGroupBox or a TPanel. If one control (parent) contains others, the contained controls are child controls of the parent.

Parent determines how the component is displayed. For example, the Left and Top properties are all relative to the Parent.

The Parent property can be assigned and changed during run-time.

Not all components have the Parent. Many forms do not have a Parent. For example, forms that appear directly on the Windows desktop have Parent set to nil. A component's HasParent method returns a boolean value indicating whether or not the component has been assigned a parent.

We use the Parent property to get or set the parent of a control. For example, place two panels (Panel1, Panel2) on a form and place one button (Button1) on the first panel (Panel1). This sets Button's Parent property to Panel1.

 Button1.Parent := Panel2; 

If you place the above code in the OnClick event for the second Panel, when you click Panel2 the button "jumps" from Panel1 to Panel2: Panel1 is no longer the Parent for the Button.

When you want to create a TButton at run-time, it is important that we remember to assign a parent - the control that contains the button. For a component to be visible, it must have a parent to display itself within.

ParentThis and ParentThat

If you select a button at design time and look at the Object Inspector you'll notice several "Parent-aware" properties. The ParentFont, for example, indicates whether the Font used for the Button's caption is the same as the one used for the Button's parent (in the previous example: Panel1). If ParentFont is True for all Buttons on a Panel, changing the panel’s Font property to Bold causes all Button's caption on the Panel to use that (bold) font.

Controls property

All components that share the same Parent are available as part of the Controls property of that Parent. For example, Controls may be used to iterate over all the children of the windowed control.

The next piece of code can be used to hide all the contained components on Panel1:

 for ii := 0 to Panel1.ControlCount - 1 do
   Panel1.Controls[ii].Visible := false;
 

Tricking tricks

Windowed controls have three basic characteristics: they can receive the input focus, they use system resources, and they can be parents to other controls.

For example, the Button component is a windowed control and cannot be the parent to some other component - you can't place another component on it. The thing is that Delphi hides this feature from us. An example is the hidden possibility for a TStatusBar to have some components like TProgressBar on it.

Ownership

First note that a Form is the overall Owner of any components that reside on it (positioned on the form at design-time). This means that when a form is destroyed, all the components on the form are also destroyed. For example, if we have an application with more that one form, when we call the Free or Release method for a form object, we do not have to worry about explicitly freeing all of the objects on that form - because the form is the owner of all it's components.

Every component we create, at design or run time, must be owned by another component. The owner of a component — the value of its Owner property — is determined by a parameter passed to the Create constructor when the component is created. The only other way to re-assign the Owner is using the InsertComponent/RemoveComponent methods during run-time. By default, a form owns all components on it and is in turn owned by the Application.

When we use the keyword Self as the parameter for the Create method - the object we are creating is owned by the class that the method is contained in - which is usually a Delphi form.

If, on the other hand, we make another component (not the form) the owner of the component, then we are making that component responsible for disposing of the object when it is destroyed.

As like any other Delphi component, custom made TFindFile component can be created, used and destroyed at run time. To create, use and free a TFindFile component at run, you can use the next code snippet :

 uses FindFile;
 ...
 var FFile : TFindFile;
 
 procedure TForm1.InitializeData;
 begin
  //form ("Self") is the Owner of the component
  //there is no Parent since this
  //is an unvisible component.
  FFile := TFindFile.Create(Self) ;
  ...
 end;
 
Note: Since the FFile is created with an owner (Form1), we don't need to do anything to free the component - it will be freed when the owner is destroyed.

Components property

All components that share the same Owner are available as part of the Components property of that Owner. The following procedure is used to clear all the Edit components that are on the form:
 procedure ClearEdits(AForm: TForm) ;
 var
   ii : Integer;
 begin
   for ii := 0 to AForm.ComponentCount-1 do
   if (AForm.Components[ii] is TEdit) then TEdit(AForm.Components[ii]).Text := '';
 end; 

"Orphans"

Some controls (such as ActiveX controls) are contained in non-VCL windows rather than in a parent control. For these controls, the value of Parent is nil and the ParentWindow property specifies the non-VCL parent window. Setting ParentWindow moves the control so that it is contained in the specified window. ParentWindow is set automatically when a control is created using the CreateParented method.

The truth is that in most cases you do not need to care about Parents and Owners, but when it comes to OOP and component development or when you want to take Delphi one step forward the statements in this article will help you to take that step faster.

©2014 About.com. All rights reserved.