Solved

How I make a component wiht their own events?

Posted on 2000-02-23
6
193 Views
Last Modified: 2010-04-04
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?
0
Comment
Question by:mariotwsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 2549585
what about looking to the TShape component of the extctrls unit?
0
 

Expert Comment

by:octavioh
ID: 2549704
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
0
 
LVL 1

Accepted Solution

by:
AttarSoftware earned 50 total points
ID: 2550063
Set up a type, eg:

type
  tMyEvent = procedure( s : string ) of object ;

and then in the private section of your component, add :

    fMyEvent : tMyEvent ;

and then in the public or protected areas, add :

    property OnSomething : tMyEvent read fMyEvent write fMyEvent ;

and then where you want the event to be fired (in some procedure in your code), add :

  if( assigned( fMyEvent ) ) then
    fMyEvent( 'Hello World' ) ;

That will set up an event, that when triggered will send the 'Hello World' string...

  Hope this points you in the right direction,

  Good luck,

  Tim.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Expert Comment

by:octavioh
ID: 2550327
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

0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2553974
Oh yeah :O)

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

Sorry about that...

Tim.
0
 

Author Comment

by:mariotwsh
ID: 2554360


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

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month7 days, 13 hours left to enroll

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question