Add OnCreate to TCustomPanel

Hi,

i noticed that tpanel has no oncreate event and thougth that it would be great if i had an oncreate event with TPanel so i wrote one, but it won't work. please tell me what is wrong?

unit PanelTest;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, ExtCtrls;

type
  TPanelTest = class(TPanel)
  private
    { Private declarations }
    FOnCreate: TNotifyEvent;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
  published
    { Published declarations }
    property OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('XNet', [TPanelTest]);
end;

{ TPanelTest }

constructor TPanelTest.Create(AOwner: TComponent);
begin
  inherited;
  if Assigned(FOnCreate) then
    OnCreate(Self);
end;

end.
LVL 1
rtielandAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

God_AresCommented:
logical..

procedure x(...);
begin
 ...
end;

var a:Tmypanel

begin
  a := tmypanel.create(self);
  a.onCreate := x;
end;

the procedure is simpely not yet bound the the component. So FonCreate will be nil.


it will be probaply wetter if yoyu override the paint method and use a new variable.

procedure paint
begin
  inherited;
  if firsttime then
  Begin
    if assigned(oncreate) then oncreate(self);
    firsttime := 1=0;
  end;
end;
0
God_AresCommented:
procedure paint
begin
 if firsttime then
 Begin
   if assigned(oncreate) then oncreate(self);
   firsttime := 1=0;
 end;
 inherited;
end;

better do this. or else onpaint comes before oncreate.
0
rtielandAuthor Commented:
God_Ares i think you do not fully understand my problem (probably because of my bad english)

if i register the component in the IDE and put the component on a form and create a procedure in the object inspectors event tab with the property OnCreate. then when i run the project the value of FOnCreate is nil and it should be set to the procedure wich i assigned to it with the Object Inspector.

here is the form (unit and dfm) using the component:

Unit:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, PanelTest;

type
  TForm1 = class(TForm)
    PanelTest1: TPanelTest;
    procedure PanelTest1Create(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PanelTest1Create(Sender: TObject);
begin
  PanelTest1.Caption := 'Hi';
end;

end.

DFM:

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object PanelTest1: TPanelTest
    Left = 144
    Top = 80
    Width = 185
    Height = 41
    Caption = 'PanelTest1'
    TabOrder = 0
    OnCreate = PanelTest1Create
  end
end
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

TOndrejCommented:
Of course FOnCreate will always be nil in the constructor.

When the components are streamed from the .dfm they are created first (you need an instance to work with ;-), then assigned properties from the stream (this is a very simplified description).

If you look at what TForm or TDataModule do: they call OnCreate from their overriden AfterConstruction method.

HTH
TOndrej
0
God_AresCommented:
yeah i understood

your component will be created then the on??? will be bound. so when creating oncreate equals nil. therefore

 if Assigned(FOnCreate) then
   OnCreate(Self);

will fail.


0
God_AresCommented:
AfterConstruction is not an option onCreate is still nil
0
TOndrejCommented:
Right. Thanks for correcting me, God_Ares.
The difference is that data modules and forms are root components for streaming.

For non-root components like TCustomPanel descendants, I suggest you override Loaded method (which is called after the component has been created and loaded from the stream).

Something like this:

type
  TCustomTestPanel = class(TCustomPanel)
  private
    FOnCreate: TNotifyEvent;
  protected
    procedure DoCreate; virtual;
    procedure Loaded; override;

    property OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
  end;

procedure TCustomTestPanel.DoCreate;
begin
  if Assigned(FOnCreate) then
    FOnCreate(Self);
end;

procedure TCustomTestPanel.Loaded;
begin
  inherited Loaded;
  DoCreate;
end;

HTH
TOndrej
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
God_AresCommented:
yep that's it.
0
God_AresCommented:
so what are root and non-root components?

root = Tcustom form may hold other components but can't be owned by other components?
0
TOndrejCommented:
Root components are those which are used as Root for streaming (have a look at TReader.Root).
Each .dfm resource has one Root.
Root is created from the stream, creating and initializing the child components, and then fixup is done (for class reference properties).
Most typically, Root is also Owner of all components; but in some cases a non-root component may also own its subcomponents.

> may hold other components but can't be owned by other
> components?

At design time and for streaming purposes, correct.
Root is the top level and cannot be owned by another component.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.