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.
- Start Delphi
- Select the first object (e.g. Button).
- Go the Events page of the Object Inspector.
- Select the event to which you want to attach a handler (e.g. OnClick).
- Write the event handler for the Button.
- Select the second object (e.g. Menu Item)
- Go to the Event page on the Object Inspector.
- Click the down arrow next to the event (OnClick)
- This will open a list of previously written event handlers.
- The list includes only event handlers written for events of the same name on the same form.
- Select the event (by clicking an event-handler name) from the drop-down list. (e.g. Button1Click)
- That's it!
- In step 6, we can select more than one object - more than two objects can share single event handler.
- 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.
More How To's from your Guide to Delphi Programming
Put this How To on your PDA!