Home

Tools API

Custom Form Constructors

Open Tools API

Introduction
Services
Wizards
Messages
Notifiers
Creators
Editors
Debugger
Examples
Custom Forms

Constructors

Home

The tricky part of writing a custom form class is defining the constructor. You must consider several different situations. First of all, do you need to load a DFM for your custom form class, or only for the classes that the user will derive from your class?

If you need to load a DFM for your class

Just call the inherited Create constructor. Let it do it's thing, including loading the DFM for your custom form.

If you don't need to load a DFM for your class

Override Create to call the inherited CreateNew, not the inherited Create.  You also need to call InitInheritedComponent to load the DFM for the derived class or classes. Here is an example of how a custom form class might define its constructor:

constructor TMyForm.Create(Owner: TComponent);
begin
  inherited CreateNew(Component);
  // initialize custom fields, etc., here
  GlobalNameSpace.BeginWriting;
  try
    if (ClassType <> TMyForm) and
       not (csDesigning in ComponentState) then
    begin
      if not InitInheritedComponent(Self, TMyForm) then
        raise Exception.Create('Oops');
      if OldCreateOrder and Assigned(OnCreate) then
        OnCreate(Self);
    end;
  finally
    GlobalNameSpace.EndWriting;
  end;
end;

You probably want to issue a better error message than "Oops."


Copyright © 1998 Tempest Software, Inc. All rights reserved.