Link to home
Start Free TrialLog in
Avatar of mariotwsh
mariotwsh

asked on

How I make a component wiht their own events?

I have to make a component that draws
lines, circles and polygons (like the line of Visual Basic). But I don`t know how to make the events for this component. What can I do?
Avatar of ZifNab
ZifNab

what about looking to the TShape component of the extctrls unit?
My recommendations:
a) Take a look at the chapters on component development in the Developer's Guide.
b) Take a look at the source code of the TShape component provided with Delphi.
c) Take a look at the example included with Delphi under
  Program Files\Delphi5\Demos\Propedit.

ThePIELIB package described there shows how to create a simple component that draws pies. This component implements several properties and an OnChange event. Furthermore, this sample shows how to build specialized property editors.

IMHO, any developer designing Delphi components should read "Developing Delphi components" by Ray Konopka. It was written for Delphi 3, but almost nothing has changed regarding component development since then.

Regards,

OH
ASKER CERTIFIED SOLUTION
Avatar of AttarSoftware
AttarSoftware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Regarding Tim' answer:

The suggested approach is on the right direction, but using the type tMyEvent as he proposes will not allow the programmer using your components to distinguish which concrete component generated the event. For instance, if an user of your component places two 'shapes' on a form, he may want to share a common event handler (so it's fired by both components), but detecting inside that method which one of the two components actually 'launched' it.

All the VCL events use a first parameter 'Sender: TObject' in event types, and Delphi automatically pushes onto the stack the address of the component that fired the event. The programmer that uses your class can then use the Sender parameter to communicate with the concrete component for which the event was fired.

The simplest event type is called TNotifyEvent, and is already defined by Delphi:

type
  TNotifyEvent = procedure(Sender: TObject) of object;

Other, more complex event handler patterns are also defined for keyboard events, mouse events, etc.

Again, consult the manual and check out the VCL source code (the biggest source of information, actually).

Regards,

OH

Oh yeah :O)

Just me being lazy, and avoiding typing the ( sender : tObject ) bit ;O)

Sorry about that...

Tim.
Avatar of mariotwsh

ASKER



   A lot of thanks!!!!!!!!!!!