1. Computing

How to Share an Event Handler in Delphi
From your Delphi Programming Guide

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




More How To's from your Guide to Delphi Programming


Put this How To on your PDA!




©2013 About.com. All rights reserved.