1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Creating Custom Delphi Components, Part I
Page 4: Adding events
 More of this Feature
• Page 1: Why, when, how
• Page 2: New...component
• Page 3: Raw Code
• Page 5: Writing standards

• Download Demo Projects
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Custom VCL development
• VCL with source
• Third party VCL
• VCL using

   Adding events
There are a few simple steps to adding events to your component. Events allow the component to communicate with your application, to notify it when something important has happened. An event is merely a read / write property, instead of being a simple variable type (such as string, integer etc) it is a procedure or function.

Create a new component, descend it from TSecondComponent and name it TThirdComponent. Save the unit, install your component, and add the following code.

type
  TState = (stStarted, stStopped);
  TStateChangeEvent = procedure
  (Sender : TObject; State : TState) of object;

  TThirdComponent = class(TSecondComponent)
  private
    { Private declarations }
    FState  : TState;
    FOnStart,
    FOnStop  : TNotifyEvent;
    FOnStateChange  : TStateChangeEvent;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Start; override;
    procedure Stop; override;
    property State : TState
      read FState;
  published
    { Published declarations }
    property OnStart : TNotifyEvent
      read FOnStart
      write FOnStart;
    property OnStateChange : TStateChangeEvent
      read FOnStateChange
      write FOnStateChange;
    property OnStop : TNotifyEvent
      read FOnStop
      write FOnStop;
  end;

Events are simply procedures or functions (rarely) that belong to a class (hence the "of object" clause you see in the TStateChangeEvent). For example, TNotifyEvent is a standard event type implemented by Delphi which just passes the object that triggered the event, it is always good to send "Self" (Sender : TObject) as the first parameter of any event as the same event code may be used for multiple components. TNotifyEvent is defined as

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

To call an event from within a component is just a case of checking if the event has been assigned and, if so, calling it. I have overridden the Start and Stop methods of TSecondComponent in order to trigger these events, like so.

procedure TThirdComponent.Start;
begin
  inherited; //This calls TSecondComponent.Start
  FState := stStarted;
  if Assigned(OnStart) then OnStart(Self);
  if Assigned(OnStateChange) then 
    OnStateChange(Self, State);
end;

procedure TThirdComponent.Stop;
begin
  inherited;  //This calls TSecondComponent.Stop
  FState := stStopped;
  if Assigned(OnStop) then OnStop(Self);
  if Assigned(OnStateChange) then 
    OnStateChange(Self, State);
end;

constructor TThirdComponent.Create(AOwner: TComponent);
begin
  inherited;
  //This is were you initialise properties, and create
  //and objects your component may use internally
  FState := stStopped;
end;

destructor TThirdComponent.Destroy;
begin
  //This is where you would destroy
  //any created objects  
  inherited;
end;

Recompile your package (don't forget to save your package anytime you add a new component). Upon dropping your new component on the form you will notice that there are three events. OnStart, OnStop, and OnStateChange. If you look at Demo3 you will see how I have used these events.

OnStart sets the caption to "Started"
OnStop shows the elapsed time
OnStateChange enables / disables the relevant Start / Stop button

procedure TForm1.ThirdComponent1Start(Sender: TObject);
begin
  Caption := 'Start';
end;

procedure TForm1.ThirdComponent1Stop(Sender: TObject);
begin
  Caption := ThirdComponent1.ElapsedTime;
end;

procedure TForm1.ThirdComponent1StateChange
  (Sender: TObject; State: TState);
begin
  btnStart.Enabled :=
     ThirdComponent1.State = stStopped;
  btnStop.Enabled :=
     ThirdComponent1.State = stStarted;
end;

Next page > Standards in component writing > Page 1, 2, 3, 4, 5

Creating Custom Delphi Components >>
>> Part II.

All graphics (if any) in this feature created by Peter Morris.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Look Ma, I've found a BUG.
How do you know if a bug in your Delphi application is really yours? Windows applications are NOT bug-free; Delphi is no exception to this rule.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.