Solved

Form Inheritance

Posted on 1998-01-08
2
300 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now