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('TBaseFor m',Self);
end;
procedure TBaseForm.tobtnExitClick(S ender: TObject);
begin
ExitItemClickAction;
end;
procedure TBaseForm.ExitItemClickAct ion;
begin
Close;
end;
procedure TBaseForm.btnCloseClick(Se nder: 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
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('TBaseFor
end;
procedure TBaseForm.tobtnExitClick(S
begin
ExitItemClickAction;
end;
procedure TBaseForm.ExitItemClickAct
begin
Close;
end;
procedure TBaseForm.btnCloseClick(Se
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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('TBaseFor
What for is this?
What for do you register a button?
Matvey