2

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.

Community
  • 1
  • 1
Arnold
  • 4,578
  • 6
  • 52
  • 91
  • You should design your own style, Default style uses regions of a big and major png file to draw buttons and other controls. See also http://stackoverflow.com/questions/18447029/how-to-change-edit-control-background-color-in-firemonkey/18455790#18455790 – mh taqia Oct 25 '13 at 16:44
  • 1
    "which type of object exactly is being returned?" : You can look up an object's ClassName in the debugger. – MartynA Oct 25 '13 at 16:55
  • 1
    Wouldn't it be easier to use a `TColorButton` instead, which directly exposes a "Color" property? (As far as "which type of object is being returned", you can use the debugger or a `ShowMessage` for the classname: `var Obj: TFmxObject; begin Obj := Button1.FindResource('background'); if Assigned(Obj) then ShowMessage(Obj.ClassName); end;` – Ken White Oct 25 '13 at 19:57
  • mmm... sometimes life is easier than one might expect. I never looked for a TColorButton :-) I immediately looked for an ImageButton or BitmapButton as well but they do not exist, sigh. Thanks for the ClassName property. If you make it an answer I'll mark it as the corect one. – Arnold Oct 25 '13 at 20:11
  • @Arnold: Often, just typing something like 'button' into the component palette's Search will be revealing. :-) – Ken White Oct 26 '13 at 00:36

2 Answers2

3

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).

Mike Sutton
  • 4,191
  • 4
  • 29
  • 42
  • I have chosen for the easy route for now, as suggested by Ken. However, your answer points to a lack of my knowledge of styles and that appears a more fundamental approach for problems I'll be facing soon like putting bitmaps on buttons and such. I'll start experimenting along the lines you suggested. Thanks a lot! – Arnold Oct 26 '13 at 11:30
  • @Arnold you can read some stuff on styles at my site at http://monkeystyler.com/guide (still more to be added at the time of writing). – Mike Sutton Oct 29 '13 at 10:48
  • Thank you for this link! This will greatly help my understanding of styles. And I found some interesting references to grids as well. Never got that working :-) – Arnold Oct 30 '13 at 19:49
1

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:

Image of two TColorButtons

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; 
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Great, both things work. The cast return TButtonStyleObject. I'll find out what it is. Thanks a lot! – Arnold Oct 26 '13 at 11:26