I am using the code below to set the properties using RTTI
with Delphi 10.2 Tokyo in components created at runtime, everything works correctly because the property of the example is the TypeLine
, because I can access it directly.
Componente_cc
Is a variable that can be instantiated with any class, be itTLabel
,TButton
,TEdit
... or any other. In the case below I'm instantiating it as being aTLine.
Var
Componente_cc: TControl;
procedure TfrmPrincipal.AlteraPropriedades;
begin
if IsPublishedProp(Componente_cc, 'LineType') then
SetPropValue(Componente_cc, 'LineType', 'Diagonal');
end;
However, I did not understand how to do when there is a sub-property, such as Stroke
, it has Kind
, Color
, Cap
, Dash
, among others. How to change the values of these properties by using the SetPropValue()
function. I have simplified the example code for a better understanding, but in the general context of my system I will need to use RTTI
, of course changing the properties directly by the code would be simple, but I do need RTTI
.
This is similar to your other RTTI issue, where you are accessing a control's
TextSettings.Font
property via RTTI. The same thing applies for any nested-property, likeStroke.Color
, etc.For each nested sub-property, you have to get the containing object, repeating as needed until you reach the desired sub-object, then you can get/set its property values as needed.
So, in this case, you have to use
GetObjectProp()
to get theStroke
property object, then you can useSetPropValue()
to set that object's properties. For example:Or, to avoid a double RTTI lookup of the named property:
Note that a more powerful Enhanced RTTI was introduced in Delphi 2010 (this RTTI is not limited to just published properties, like old-style RTTI is), for example:
However, it is better to just access sub-properties directly once you have access to a higher-level object, for example:
Or: