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:
- 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.
Related Features:
- Let's talk about the Sender - Delphi For Beginners: So, what is that Sender, Delphianers are talking about, anyway?
- More Delphi Programming HOW TOs

