I was looking at all the examples shipped with RAD Studio xe6 when I came across the custom listbox example located at
Object Pascal > FireMonkey Desktop > CustomListBox
Trying to play with it and see what modifications I could do, I decided that it I wanted to create a for loop that would get the visible or not property for each object. The thing though is that I can't understand what the following line actually means.
107| Item.StylesData['visible.OnChange'] := TValue.From<TNotifyEvent>(DoVisibleChange); // set OnChange value
It adds an onChange event, but how exactly? What is TNotifyEvent, is that how we tell the compiler to create a new event?
Thanks.
The FireMonkey styles framework has been designed to be flexible and extensible. It is introduced at the root of the styled control hierarchy, TStyledControl
. This article gives a brief introduction and explanation of the philosophy behind the design.
Because the styling framework is designed to support many different types of controls, there is a clear need for flexibility and extensibility. So you see code like this:
StylesData['visible.OnChange'] := ...
The StylesData
property is an array property, indexed with a string. It is declared like this:
property StylesData[const Index: string]: TValue;
The TValue
type is the modern variant type that is used throughout the RTL. So, we gain flexibility by allowing StylesData
to hold any kind of object, by means of using a variant type, TValue
. And we have extensibility by allowing named indices.
So, the control that you are referring to allows you to customise its behaviour when its visibility changes. It does so by checking for a style named visible.OnChange
that is expected to be of type TNotifyEvent
. We cannot supply the TNotifyEvent
directly, we have to wrap it in a TValue
. And hence the call to TValue<TNotifyEvent>.From()
.