Reputation: 4850
My Application has several TSpeedButtons
with which to choose a color and I want each choice to be shown by the color of the TSpeedButton
.
I found this answer on Stackoverflow on how to change the color of a TButton
. The second answer (change colors dynamically) appears to be the solution I am looking for. It reads as follows:
var r: TRectangle;
begin
// Find the background TRectangle style element for the button
r := (Button1.FindStyleResource('background') as TRectangle);
if Assigned(r) then
begin
r.Fill.Color := claBlue;
end;
end;
This does not work anymore (i use XE5, this is XE2?). It generates an exception at the r := ...
statement with:
"illegal cast".
The FindStyleResource returns a FMXObject.
TRectangle is a TShape->TControl->TFMXObject.
I can cast to TControl but not to TShape. In case you wonder, Button1 is a TButton.
Does anyone know how I do change the color of a TSpeedButton
?
As an aside: is there a way to determine which type of object exactly is beging returned? I couldn't find out in the debugger.
Upvotes: 2
Views: 7732
Reputation: 125748
It would be much easier to use a TColorButton
instead, which directly exposes the Color
property. You can find it on the Colors
page of the component palette. Here are two on a new FMX form in the IDE's form designer:
As far as "which type of object is being returned", you can use the debugger or a ShowMessage
for the TFMXObject.ClassName
of the return value:
var
Obj: TFmxObject;
begin
Obj := Button1.FindResource('background');
if Assigned(Obj) then
ShowMessage(Obj.ClassName);
end;
Upvotes: 1
Reputation: 4211
The answer to the question you linked to relates to vector styles, where the style constructed entirely from shapes etc (such as the TRectangle).
In newer versions of FireMonkey the 'system' styles (which mimic the OS look) and some other styles use bitmaps.
If you want to edit a bitmap style, you'll need to find the bitmap image in the style, edit it, and then redo/edit the button's style to use the new image. (If you're on mobile this will probably be hard enough that you shouldn't even try it).
Another route would be be to change to one of the bitmap styles supplied with Delphi. You will find them under the redist/styles/fmx folder of your Delphi installation.
As for the class of the object, and as per other comments, examine the ClassName property of the object returned.
But bear in mind that not every style will have an object called 'background'. Both the name of the object and it's class can easily vary between styles. You really ought to look at the style you want to pluck objects from to see what's there. (Note that the objects name ('background') will be in the StyleName property).
Upvotes: 3