0

I worked through the example posted here as my starting point: Change background of TTextCell in a Firemonkey TGrid

I have created a textcellstyle which references an image, and this is working well. When I run the program all the cells display the background image as expected.

From the above link, Mike Sutton (I hope you're reading this, what would we do without your input!) writes (repeated here just to make it easier):

"You can then set each of your cells StyleLookup properties to use it, or set the styles StyleName to TextCellStyle to have it picked up automatically for every TTextCell."

Following on from the query about changing the font colours (Delphi XE4 Firemonkey Grid Control - Styling cells individually), can one set the background colours dynamically as well?

My code on creating the cells:

Constructor TFinancialCell.Create(AOwner:TComponent);

begin
  inherited;
  StyleLookup:='textcellstyle';
  StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; 
  TextAlign:=TTextAlign.taTrailing;
end;

This applies my image successfully to TFinancialCell.

But, as per the font colour query, I would like the image background to display only when a certain value is reached or whatever:

Procedure TFinancialCell.ApplyStyling;
begin
  Font.Style:=[TFontStyle.fsItalic];

  If IsNegative then
    FontColor:=claRed
  else
    FontColor:=claGreen;

  If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; 
  If Assigned(Font.OnChanged) then
    Font.OnChanged(Font);

  Repaint;
end;

Any help as to how to do this would be appreciated.

Community
  • 1
  • 1
Alex
  • 543
  • 1
  • 9
  • 21
  • In theory, and assuming the style doesn't use bitmaps, in ApplyStyle, call `FindStyleResource('background')` which should return a TRectangle and you can change it's Fill.Color. If that fails I'll take a look when I get time. – Mike Sutton May 15 '13 at 12:42

1 Answers1

0

Thanks Mike. I had to fiddle around a bit, but got it to work based on your suggestion. I added a TRectangle to my textcellstyle in the stylecontainer, as follows:

textcellstyle : TLayout
    background: TSubImage
        rectangle1: TRectangle
        rectanimation: TRectAnimation

In TFinancialCell.ApplyStyle I tried FindStyleResource ('background'), but this always returned nil. I changed it to FindStyleResource ('rectangle1') and this worked great. Is this because it looks for the relevant StyleName property (which obviously defaults to 'Rectangle1' for rectangle 1) in the object inspector? Still not quite seeing the wood for the trees, as I'm sure you can tell...

The working code:

Procedure TFinancialCell.ApplyStyle;

var 
  T : TFMXObject;

begin
  inherited;

  T:=FindStyleResource('Rectangle1');

  If (T<>nil) and (T is TRectangle) then
  begin 
    If TRectangle(T).Fill<>nil then 
    begin 
      If IsNegative then 
      begin
        TRectangle(T).Fill.Color:=claRed; 
        Repaint;
      end;  
    end;
  end;

  ApplyStyling;
end;

I also tried, as a separate exercise, to put the code above in TFinancialCell.ApplyStyling, and it also worked there, so not sure which is the better option, and why?

The summary of my understanding of these styles so far is (please correct/comment as necessary):

  1. I have created a style called textcellstyle, which I apply in TFinancialCell.Create to my TFinancialCell class [StyleLookup:='textcellstyle'].
  2. When I call TFinancialCell.ApplyStyling, I am able to access the Font and FontColor properties of TFinancialCell directly, as these properties are properties of TTextCell.
  3. If I want to paint the background of the cells, I have to explicitly call the TRectangle component that I manually added to the textcellstyle 'style', and then access the Fill etc property from there.
Alex
  • 543
  • 1
  • 9
  • 21
  • 1. It should find your style without needing to set StyleLookup. If it doesn't call your style FinancialCellStyle. 2. Correct. 3. I was assuming their was already a TRectangle in the style and it was called background. If there isn't things could get complicated, e.g. if the TSubImage you have above is from the TextCellStyle. – Mike Sutton May 16 '13 at 14:00
  • In your code above: just test if T is TRectangle (which will fail if T is nil); Fill will never be nil so no need to test; No need to call Repaint from ApplyStyle, which is called before the control is painted. – Mike Sutton May 16 '13 at 14:02
  • Your style shows background as a TSubImage, but you say FindStyleResource returns nil which doesn't make sense. If the TSubImage is from the TextCellStyle I suggest replacing it with a TRectangle and replacing the animations below it for any effects you want to apply. (You'll probably want TColorAnimations to change the Fill.Color). – Mike Sutton May 16 '13 at 14:04
  • Hi Mike, thanks. I made the code changes (2nd comment) you suggested, all good. In terms of the stylecontainer issues, those are probably silly things I am doing, I had focused more on the code till now. I've now had a look, still getting a bit stuck on this. I think I started with 'editstyle', and renamed that to 'textcellstyle', so whatever is under the tree belonged to 'editstyle' initially. Then somehow (from reading a variety of forum posts), I dragged a TRectangle under the 'background' tree, as shown above. This works well.... – Alex May 17 '13 at 10:10
  • ....I have now, based on your queries, experimented a bit with moving the rectangle up the tree, renaming it, etc, to see what effect it has. In short, I can rename 'background' to whatever I like, as long as 'background' is a TSubImage and the rectangle is below that in the tree - in this case all works fine. Other combinations either produce no background or font, or both. I have not yet moved the rectangle out of textcellstyle altogether, is this what you mean? – Alex May 17 '13 at 10:16