TComponent

Hi all. Can somebody write me simple Component, which will have one property
and one Event? I just want to learn
writting it, and to see it's code structure.
Thanks.
zekoAsked:
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.

dygCommented:
unit Unit1;

interface

uses Classes;

type
  TMyComponent = class(TComponent)
  private
    FMyProp: Integer;
    FMyEvent: TNotifyEvent;
    procedure SetMyProp(const Value: Integer);
  public
    procedure EventExample;
  published
    property MyProp: Integer read FMyProp write SetMyProp;
    property MyEvent: TNotifyEvent read FMyEvent write FMyEvent;
  end;
implementation

{ TMyComponent }

procedure TMyComponent.EventExample;
begin
  if Assigned(FMyEvent) then FMyEvent(Self);
end;

procedure TMyComponent.SetMyProp(const Value: Integer);
begin
  FMyProp := Value;
end;

end.
0
RadlerCommented:
If you have a Pro + Delphi version the VCL code goes too.
See the VCL code( check if you installed them ) to learn more.

T++, Radler.
0
zekoAuthor Commented:
I tried, but the realy problem is
how to make my event work.
For example I want to make it work
when component ic created. We can call that event AfterCreated or etc.
Did u understand me ?

MY problem is to make events working....

Please help me.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dygCommented:
If I understood you you need override TComponent.Loaded method. This method allows a component to initialize itself after all its parts have been loaded from a stream.
0
zekoAuthor Commented:
Yes, something like that.
0
zekoAuthor Commented:
Just to see how some event work.
0
jcasteelCommented:
You always need some places to trigger the events and I guess that is your problem. If you wabt to execute an event after the component is created... Override the create constructor, call inherited and and the end use
if Assigned(FMyEvent) then FMyEvent(Self);



contructor Create( aOwner : TComponent );
begin
  inherited // very important
  // do
  // your stuff here
  //
if Assigned(FMyEvent) then FMyEvent(Self);
end;
 
If tou want to execute something accoring to something that happened in windows like a mouse movement or something like that you well need to trap the windows events. There are a hole bunch of them but i will give you one example to work with...

type
  TClickShape = class( TShape )
private
  FrightClick : TNotifyEvent;
protected
  procedure CMRightDblClick( var message : TMessage ); message WM_RBUTTONDBLCLK;
Public

Published
  property OnRightDblClick : TNotifyEvent read FRightDblClick write FRightDblClick;
end;

procedure register;

implementation

procedure register;
begin
  RegisterComponents('Component' , [TClickShape] );
end;

procedure TClickShape.CMRightDblClick( var Message : TMessage );
Begin
  if assigned( FRightDblClick ) then
  FRightDblClick( Self ) ;
  inherited;
End;

end.

There are a lot of different windowsMessages and eventhandlers in TComponent, the art of componentbuilding is finding the right spots to do the right things....


Good luck
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
ptmcompCommented:
You don't have to catch messages, if you just want to get the mouse events. There are protected methods like MouseDown, MouseUp, MouseMove on every control. They works right also for TGraphicControls.
0
jcasteelCommented:
What about the double right click?
As far as I know there is no standard event for that, and thats where the code is for....

But indeed, mouse events are very standard events  but i will give another example mouse wheel events wich is the WM_MOUSEWHEEL windows message
0
zekoAuthor Commented:
Thanks.
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.