Working With Components in Delphi

Many components are provided in the IDE on the Component palette. You select components from the Component palette and drop them onto a form or data module. You design the application’s user interface by arranging the visual components such as buttons and list boxes on a form.

You can also place nonvisual components such as data access components on either a form or a data module. At first glance, Delphi’s components appear to be just like any other classes. But there are differences between components in Delphi and the standard class hierarchies that many programmers work with. Some differences are described here:

  • All Delphi components descend from TComponent.
  • Components are most often used as is and are changed through their properties, rather than serving as “base classes” to be subclassed to add or change functionality. When a component is inherited, it is usually to add specific code to existing event handling member functions.
  • Components can only be allocated on the heap, not on the stack.
  • Properties of components intrinsically contain runtime type information.
  • Components can be added to the Component palette in the IDE and manipulated on a form.

Components often achieve a better degree of encapsulation than is usually found in standard classes. For example, consider the use of a dialog containing a push button. In a Windows program developed using VCL components, when a user clicks on the button, the system generates a WM_LBUTTONDOWN message.

The program must catch this message (typically in a switch statement, a message map, or a response table) and dispatch it to a routine that will execute in response to the message.

Most Windows messages (VCL applications) or system events (CLX applications) are handled by Delphi components. When you want to respond to a message or system event, you only need to provide an event handler.

Setting Component Properties

To set published properties at design time, you can use the Object Inspector and, in some cases, special property editors. To set properties at runtime, assign their values in your application source code.

When you select a component on a form at design time, the Object Inspector displays its published properties and (when appropriate) allows you to edit them. Use the Tab key to toggle between the left-hand Property column and the right-hand Value column.

When the cursor is in the Property column, you can navigate to any property by typing the first letters of its name. For properties of Boolean or enumerated types, you can choose values from a drop-down list or toggle their settings by doubleclicking in Value column.

If a plus (+) symbol appears next to a property name, clicking the plus symbol or typing ‘+’ when the property has focus displays a list of subvalues for the property. Similarly, if a minus (-) symbol appears next to the property name, clicking the minus symbol or typing ‘-’ hides the subvalues.

By default, properties in the Legacy category are not shown; to change the display filters, right-click in the Object Inspector and choose View. For more information, see “property categories” in the online Help.

When more than one component is selected, the Object Inspector displays all properties—except Name—that are shared by the selected components. If the value for a shared property differs among the selected components, the Object Inspector displays either the default value or the value from the first component selected. When you change a shared property, the change applies to all selected components.

Changing code-related properties, such as the name of an event handler, in the Object Inspector automatically changes the corresponding source code. In addition, changes to the source code, such as renaming an event handler method in a form class declaration, is immediately reflected in the Object Inspector.

Some properties, such as Font, have special property editors. Such properties appear with ellipsis marks (...) next to their values when the property is selected in the Object Inspector. To open the property editor, double-click in the Value column, click the ellipsis mark, or type Ctrl+Enter when focus is on the property or its value.

With some components, double-clicking the component on the form also opens a property editor. Property editors let you set complex properties from a single dialog box. They provide input validation and often let you preview the results of an assignment.