Delphi Component Properties

For the most part, component properties are straight forward, but there are a few tricks you should know.

Overview | Pick List | TStrings | Using Functions as Properties
Arrays | Default Property Values | Testing for Design Time | Updating the component


Overview

Generally speaking, a property consists of 2 parts There are many variations on this Published properties are available at design time via the Object Inspector - just click on the property and enter some data (in fact, this is the major use of properties). Public properties are only available at run time.

All properties should be assigned initial values in the component's overridden Create method.


Properties with a pick list

Sometimes properties present a list of available options. This is accomplished by defining an enumerated type and then defining a property of that type.


TStrings

At design time, properties based on TStrings provide a way to enter several lines of text. You click on the property, then click on the button with 3 dots, and a text editor window opens - just type whatever you want. (Click Code Editor... to get a bigger edit window.)

Several components (such as TMemo and TRichEdit) have a property of this type. However ... because TStrings is an abstract class you can not CREATE a property of this type.

The secret (based on examining code and the help example for TStrings.Assign) is to define the property to be of type TStrings, but to create the property using TStringList (because it implements the TStrings abstract methods). Since the component creates the TStringList, be sure that it also Frees it. The other part of the secret is that you must Assign values to the property.

Warning: If you define the property to simply read and write a TStrings field, you will get wierd bpl failures (Access violation ... in ... VCL50.bpl). Once data is assigned to the property, you will not be able to save, compile, or close applications that use the component. The following line causes this type of problem. Note: To type a tab character in a TStrings property, use ctrl-Tab.

By default, TMemo sets the Text property default value and TRichEdit sets the Lines property default value to the name of the component, but I have not been able to figure out how that works.


Using Functions as Properties

This code example from controls.pas shows how to create an event and how to test if the event is associated with an actual function before calling it. Connecting the event to a Windows message requires additional code similar to this. Sometimes, Delphi uses a protected DoXyz to execute events (from forms.pas).


Some Pre-defined Events

There are many pre-defined events (function types) that you can use, these are just the ones I am most likely to reuse.

From classes.pas

Controls.pas - contains 18 common windows events


Arrays

Array properties accept any number of parameters of any type (not just integers as with regular arrays).

This is how it is defined.

This is how it is called


Default Property Values

The default directive merely tells the compiler whether or not to store the current value of a published property in the associated *.dfm file. It does not actually initialize the property to that value. The default value must be assigned in the constructor.

If the default directive is omitted, all values are stored in the *.dfm file. When present, if the property's value matches the default, it is not stored.

You can not use the default directive with strings. In this case, any string, except null (''), is automatically stored in the *.dfm file.


Testing for Design Time

The constructor is executed during both Design time and Run time. Normally this is where the component should set the default values for all the published properties. However, there are a few instances where this fails at design time. In these cases, you can use the following test.


Updating the component

The rules for developing components and using components are slightly different.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Delphi / ComponentProperties.html