1. Home
  2. Computing & Technology
  3. Delphi Programming

Adding Custom Properties to Delphi Forms; Overriding the Create Constructor

By , About.com Guide

The OnCreate event for Delphi forms (and data modules) gets fired when the the form is created. For dynamically created forms (those not listed in the Project - Options - Auto-create Forms list) the creation is controlled by your code - by calling the Create constructor.

A Delphi form can be dynamically created using either Appliction.CreateForm(TFormClass, FormName) or FormName := TFormClass.Create(Owner). When the form is created its OnCreate event is fired.

In most cases, you place initialization code in the OnCreate event handler.

Custom Property

If you need to add a custom property to a form and have it initialized *before* the OnCreate event, you will need to override the form's constructor.

If, for example, you need to override the constructor of Delphi form to initialize your own added form's property, here's what to do:

  • Define the property for the form,
  • Override the Create constructor,
  • Initialize your property before calling "inherited Create;"
  • Use your initialized property in the form's OnCreate event handler.
interface

type
  TRunTimeForm = class(TForm)
    procedure FormCreate(Sender: TObject) ;
  private
    fMyPreCreateValue: boolean;
  public
    constructor Create(AOwner: TComponent) ; override;
    property MyPreCreateValue : boolean read fMyPreCreateValue;
  end;

implementation

constructor TRunTimeForm.Create(AOwner: TComponent) ;
begin
  fMyPreCreateValue := true;
  inherited Create(AOwner) ;
end;

procedure TRunTimeForm.FormCreate(Sender: TObject) ;
begin
  // MyPreCreateValue was initialized before the form was created!
  ShowMessage(BoolToStr(MyPreCreateValue,true)) ;
end;

Note: you can not use any code that references the form's "standard" properties or any methods / properties for components owned by the form before calling "inherited create". You, however, *can* initialize your own fields (properties) as we did above.

Delphi tips navigator:
» Remove Delphi's MDI Child Form Title Bar
« Disable Automatic Hint Feature for the TTreeView

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. Adding Custom Properties to Delphi Forms; Overriding the Create Constructor

©2009 About.com, a part of The New York Times Company.

All rights reserved.