1. Technology

How to Implement the OnCreate event for a Delphi TFrame object

Adding TFrame.OnCreate


Implementing TFrame.OnCreate

Implementing TFrame.OnCreate

TFrame is a container for components; it can be nested within forms or other frames.

A frame, like a form, is a container for other components. Frames can be nested within forms or other frames, and they can be saved on the Component palette for easy reuse.

Missing OnCreate!

Once you start using frames, you'll note there's no OnCreate event you can use to initialize your frames.

In short, the reason that a frame does not have a OnCreate event is there is no good time to fire the event.

However, by overriding the Create method you can mimic the OnCreate event. After all, the OnCreate for Forms gets fired at the end of the Create constructor - so overriding Create for Frames is as having the OnCreate event.

Here's the source code of a simple frame exposing a publuc property and overriding the Create constructor:

 unit WebNavigatorUnit;
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, StdCtrls;
   TWebNavigatorFrame = class(TFrame)
     urlEdit: TEdit;
     fURL: string;
     procedure SetURL(const Value: string) ;
     constructor Create(AOwner: TComponent) ; override;
     property URL : string read fURL write SetURL;
 {$R *.dfm}
 constructor TWebNavigatorFrame.Create(AOwner: TComponent) ;
   inherited Create(AOwner) ;
   //"OnCreate" code
   URL := 'http://delphi.about.com';
 procedure TWebNavigatorFrame.SetURL(const Value: string) ;
   fURL := Value;
   urlEdit.Text := Value;
The "WebNavigatorFrame" acts as a web site launcher hosting an Edit and a Button control. Note: if you are new to frames, make sure you read the following two articles: Visual Component Development Using Frames , Replacing TabSheets with Frames

Delphi tips navigator:
» How to Hide / Show the Main Menu
« Apply Custom Transparent Color for the TImage Delphi Control

©2014 About.com. All rights reserved.