• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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.
0
zeko
Asked:
zeko
  • 4
  • 2
  • 2
  • +2
1 Solution
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now