Changing the Color of a FireMonkey Edit box
The new FireMonkey components have extensive design time functionality through the style engine so the presentation of components can be easily varied by the style editor. Some of the style parameters are also included in the object properties such as Font and FontFill (the new way to change the font color) but if the object code does not expose the parameter you need how do you change the style information dynamically at runtime? I came across this problem with a TEdit control. When a user puts unacceptable data into an edit box or fails to populate mandatory fields I like to be able to highlight these by changing the background to yellow. When converting this code to FireMonkey I was unable to find a Color property for a TEdit Box.
I have now produced a simple routine which achieves this function and in the process learnt a lot more about the style engine.
My solution consisted of finding the appropriate Style Object and changing the color value there. Fortunately the background rectangle of the TEdit style has a “style name” in the standard implementation which makes recovery of this object easy using the FindStyleResource method of the TEdit object. Do not use the generic FMX.Types.FindStyleResource or you may end up changing the color of the wrong component.
From the Style Text
object TRectangle StyleName = 'background'
Having obtained the correct (TRectangle) object it is possible to change the color property and repaint the component.
procedure SetEditControlColor(AEditControl: TCustomEdit; AColor: TAlphaColor); var T: TFmxObject; begin if AEditControl = nil then Exit; T := AEditControl.FindStyleResource('background'); if (T <> nil) and (T is TRectangle) then if TRectangle(T).Fill <> nil then TRectangle(T).Fill.Color := AColor; AEditControl.Repaint; end;
This only works because the background rectangle had a unique style name. Many objects in the style file do not and if you need to modify those objects you could apply your own style file assigning a style name to access the style object but that is not nearly so attractive. Style names seem to be assigned so that the component code can get access to style objects so maybe the intention was always there to expose either the background rectangle or its color as TEdit properties and they may appear in the future.
The fact that component code relies on information in the style file in this way also raises the possibility that editing a style file could inadvertently block some component functionality.