1. Home
  2. Computing & Technology
  3. Delphi Programming
How To Share an Event Handler in Delphi

Associating an event with an existing event handler in Delphi in purpose of code reuse. Suppose we want to have a button and a menu item do the same thing.

Difficulty Level: easy      Time Required: 5 minutes


Here's How:
  1. Start Delphi
  2. Select the first object (e.g. Button).
  3. Go the Events page of the Object Inspector.
  4. Select the event to which you want to attach a handler (e.g. OnClick).
  5. Write the event handler for the Button.
  6. Select the second object (e.g. Menu Item)
  7. Go to the Event page on the Object Inspector.
  8. Click the down arrow next to the event (OnClick)
  9. This will open a list of previously written event handlers.
  10. The list includes only event handlers written for events of the same name on the same form.
  11. Select the event (by clicking an event-handler name) from the drop-down list. (e.g. Button1Click)
  12. That's it!
Tips:
  1. In step 6, we can select more than one object - more than two objects can share single event handler.
  2. The procedure above is an easy way to reuse event handlers. Action lists (Delphi 4 and above), however, provide a more powerful tool for centrally organizing the code that responds to user commands.

Related Features:

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

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

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

All rights reserved.