Working With Events and Event Handlers in Delphi

Almost all the code you write is executed, directly or indirectly, in response to events. An event is a special kind of property that represents a runtime occurrence, often a user action. The code that responds directly to an event—called an event handler—is a Delphi procedure.

You can generate skeleton event handlers for forms and other components. To create an event handler:

  1. Select a component.
  2. Click the Events tab in the Object Inspector. The Events page of the Object Inspector displays all events defined for the component.
  3. Select the event you want, then double-click the Value column or press Ctrl+Enter. The Code editor opens with the cursor inside the skeleton event handler, or begin...end block.
  4. At the cursor, type the code that you want to execute when the event occurs.

Some components have a default event, which is the event the component most commonly needs to handle. For example, a button’s default event is OnClick.

To create a default event handler, double-click the component in the Form Designer; this generates a skeleton event-handling procedure and opens the Code editor with the cursor in the body of the procedure, where you can easily add code.

Not all components have a default event. Some components, such as TBevel, don’t respond to any events. Other components respond differently when you double-click them in the Form Designer. For example, many components open a default property editor or other dialog when they are double-clicked at design time.

If you generated a default event handler for a component by double-clicking it in the Form Designer, you can locate that event handler in the same way. Double-click the component, and the Code editor opens with the cursor at the beginning of the eventhandler body. To locate an event handler that’s not the default:

  1. In the form, select the component whose event handler you want to locate.
  2. In the Object Inspector, click the Events tab.
  3. Select the event whose handler you want to view and double-click in the Value column. The Code editor opens with the cursor inside the skeleton event-handler.

You can reuse code by writing event handlers that respond to more than one event. For example, many applications provide speed buttons that are equivalent to dropdown menu commands.

When a button initiates the same action as a menu command, you can write a single event handler and assign it to both the button’s and the menu item’s OnClick event. To associate an event with an existing event handler:

  1. On the form, select the component whose event you want to handle.
  2. On the Events page of the Object Inspector, select the event to which you want to attach a handler.
  3. Click the down arrow in the Value column next to the event to 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 from the list by clicking an event-handler name.

The previous procedure is an easy way to reuse event handlers. Action lists and in the VCL, action bands, however, provide powerful tools for centrally organizing the code that responds to user commands. Action lists can be used in cross-platform applications, whereas action bands cannot.

In an event handler, the Sender parameter indicates which component received the event and therefore called the handler. Sometimes it is useful to have several components share an event handler that behaves differently depending on which component calls it. You can do this by using the Sender parameter in an if...then...else statement.

When components share events, you can display their shared events in the Object Inspector. First, select the components by holding down the Shift key and clicking on them in the Form Designer; then choose the Events tab in the Object Inspector.

From the Value column in the Object Inspector, you can now create a new event handler for, or assign an existing event handler to, any of the shared events.

The Menu Designer, along with the MainMenu and PopupMenu components, make it easy to supply your application with drop-down and pop-up menus. For the menus to work, however, each menu item must respond to the OnClick event, which occurs whenever the user chooses the menu item or presses its accelerator or shortcut key.

To create an event handler for a menu item:

  1. Open the Menu Designer by double-clicking on a MainMenu or PopupMenu component.
  2. Select a menu item in the Menu Designer. In the Object Inspector, make sure that a value is assigned to the item’s Name property.
  3. From the Menu Designer, double-click the menu item. The Code editor opens with the cursor inside the skeleton event handler, or the begin...end block.
  4. At the cursor, type the code that you want to execute when the user selects the menu command.

To associate a menu item with an existing OnClick event handler:

  1. Open the Menu Designer by double-clicking a MainMenu or PopupMenu component.
  2. Select a menu item in the Menu Designer. In the Object Inspector, make sure that a value is assigned to the item’s Name property.
  3. On the Events page of the Object Inspector, click the down arrow in the Value column next to OnClick to open a list of previously written event handlers. (The list includes only event handlers written for OnClick events on this form.) Select from the list by clicking an event handler name.

When you delete a component from a form using the Form Designer, the Code editor removes the component from the form’s type declaration. It does not, however, delete any associated methods from the unit file, since these methods may still be called by other components on the form.

You can manually delete a method—such as an event handler—but if you do so, be sure to delete both the method’s forward declaration (in the unit’s interface section) and its implementation (in the implementation section). Otherwise you’ll get a compiler error when you build your project.