RAD Studio
ContentsIndex
PreviousUpNext
Working with components
Name 
Description 
You can install custom components—written by yourself or third parties—on the Tool palette and use them in your applications. To write a custom component, see Overview of component creation. To install an existing component, see Installing component packages
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. This topic explains how to associate event handlers with menu items. For information about the Menu Designer and related components, see Creating and managing menus
Methods are called just like ordinary procedures and functions. For example, visual controls have a Repaint method that refreshes the control's image on the screen. You could call the Repaint method in a draw-grid object like this:  
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. 
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. 
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 drop-down 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. 
You can generate skeleton event handlers for forms and other components. 
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... more 
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 event-handler body. 
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 double-clicking in Value column.
If a plus (+) symbol appears next to a property name, clicking... more 
You can set component properties at design time or at runtime:
  • 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
For information about the properties of each component, see the VCL library in the online Help. 
Any writable property can be set at runtime in your source code. For example, you can dynamically assign a caption to a form:  
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. 
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. For example, the following code displays the title of the application in the caption of a dialog box only if the OnClick event was received by Button1.  
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. The sections that follow show how to:  
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!