Link to home
Start Free TrialLog in
Avatar of alanward
alanward

asked on

Form Inheritance

I am trying to create a base form so that I can have a standard base for all of the forms in a project. The code for the BaseForm is as follows:-

unit main;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  ToolWin,
  Menus,
  StdCtrls;

type
  TBaseForm = class(TForm)
    btnClose: TButton;
    procedure tobtnExitClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
  private

  published
    procedure ExitItemClickAction; virtual;

  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  BaseForm: TBaseForm;

implementation

{$R *.DFM}

constructor TBaseForm.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
    RegisterClasses([TButton]);
    if ClassName <> 'TBaseForm' then
    ReadComponentRes('TBaseForm',Self);
end;


procedure TBaseForm.tobtnExitClick(Sender: TObject);
begin
    ExitItemClickAction;
end;

procedure TBaseForm.ExitItemClickAction;
begin
    Close;
end;

procedure TBaseForm.btnCloseClick(Sender: TObject);
begin
    ExitItemClickAction;
end;

end.

This compiles and runs properly. However when I create a derived form and run that I get an error that tells me that the btnClose already exists and so the form will not compile. This is the code for the derived form:-

unit derivedfrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, main;

type
  TDerivedForm = class(TBaseForm)
    Memo1: TMemo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DerivedForm: TDerivedForm;

implementation

{$R *.DFM}


end.


What am I doing wrong?

Thanks in anticipation.

Alan Ward
Avatar of Matvey
Matvey

Sorry, but I don't understand your Create method.
Registering components - what for? You must understand that all the instances of your form are created by this method, and register the same thing - it might be the problem, but I'm not sure.

The error itself occures in the last line of this method:
          ReadComponentRes('TBaseForm',Self)

What for is this?

What for do you register a button?

Matvey
ASKER CERTIFIED SOLUTION
Avatar of miv
miv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial