I have 2 objects of TControl. Is there in Delphi a function available that compares the objects and says or they are exactly the same, or not ? The objects always are both of the same type (for example: both TLabel, or both TImage).
Who can help me or can give me some advice ? thanks a lot. (I thought there was a
custom function in Delphi, but i haven't found one yet !)
As DelphiLand is an educational site, let's start with a short refresher ;-)
- Components are objects, that you use as building blocks. Delphi's VCL (Visual
Component Library) contains components, and you can create your own. Examples: TLabel,
- Properties are attributes of a component, that you can read or set. Examples:
Caption, Color, Height.
So, with "comparing components" you probably mean "comparing their properties". I didn't
find a Delphi-defined function for this, so you must make your own. Firstly, you need
a list of the properties that you want to compare. If it's only a limited number of component
types, say only TLabels, TEdits and TButtons, you can get such a list from Delphi's help,
from the "Object Browser" or from the components source code files. Your code could
look like this:
function CompareComps(C1, C2: TComponent): Boolean;
if C1 is TLabel then
Result := ((C1 as TLabel).Color) = (C2 as TLabel).Color))
and ((C1 as TLabel).Caption) = (C2 as TLabel).Caption))
and ((C1 as TLabel).Font) = (C2 as TLabel).Font))
if C1 is TEdit then
Result := ((...
if C1 is TButton then
Result := ((...
Though such code is quite lengthy and boring, it is easy enough for a small number
of component types. But what if you want to deal with *all* the VCL components, let
if you also include some new controls of your own?
A component can be written to disk, "streamed", and it can be read back. So, there
are possibilities to access a list of its properties. Look for topics such as "TReader" and "streaming" (TReader
is used internally by the VCL, to restore a component that has been written to a "stream").
This stuff can get quite heavy ;-) so I'm not going into more details here.
You also have to take a dive into the RTTI, "Run Time Type Information". Delphi itself
uses the RTTI to access properties when saving and loading form (.DFM) files, and to
display attributes in the Object Inspector. Attention: RTTI is only available for "published" properties,
not for protected or public properties.
There is very little information about RTTI in the official Delphi documentation,
you'll have to explore other sources like programmers magazines, web sites and newsgroups.
you have the Delphi sources, have a look at the file TYPINFO.PAS, especially the function "GetPropList".
Good introductions to the RTTI have been written by the well-known author of Delphi
books Marco Cantu.