Solved

Form Inheritance

Posted on 1998-01-08
2
305 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:alanward
2 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1355946
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
0
 
LVL 1

Accepted Solution

by:
miv earned 100 total points
ID: 1355947
Move your private declaration up in front of the vars and procedures.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question