Recent

Author Topic: [SOLVED] Font in StringGrid: cannot set different fonts per cell  (Read 449 times)

paxnet_be

  • New Member
  • *
  • Posts: 25
Hello guys,

In the attached project, I created an enhanced stringgrid with an associated object containing custom cell properties and a popup menu to change these cell properties.

Changing the color for each cell works perfectly. Changing the font for one cell works also well, but when I change the font for another cell, this change is also applied for all preceding changed cells instead of only the selected cell.

How can I solve this problem?

--------------
Lazarus 4.4 (rev lazarus_4_2-103-g98f9c7a7a1) FPC 3.2.2 x86_64-linux-gtk2
« Last Edit: April 27, 2026, 02:34:12 pm by paxnet_be »
Now I know you never know, but at least I know this. - Jean Gabin

jamie

  • Hero Member
  • *****
  • Posts: 7705
Re: Font in StringGrid: cannot set different fonts per cell
« Reply #1 on: April 22, 2026, 06:14:58 pm »
Use OnPrepairCanvas and set the font there beore each cell gets drawn
The only true wisdom is knowing you know nothing

paxnet_be

  • New Member
  • *
  • Posts: 25
Re: Font in StringGrid: cannot set different fonts per cell
« Reply #2 on: April 23, 2026, 06:28:29 am »
Thank you for reply, Jamie.

I tought, that is what I do:

- The contextmenu procedure MIChangeFontPropsClick set the font in the property HighlightedCellFont.
- The at runtime event TableGridPrepareCanvas calls the property HighlightedCellFont.

I removed InvalidateCell() in MIChangeFontPropsClick... Same result.
Now I know you never know, but at least I know this. - Jean Gabin

paweld

  • Hero Member
  • *****
  • Posts: 1616
Re: Font in StringGrid: cannot set different fonts per cell
« Reply #3 on: April 23, 2026, 08:43:12 am »
TFont is a class, so a simple assignment creates a pointer to the original class, and changing it affects all instances.
Therefore, you must create a new instance of the class for each cell.
Additionally, TStringGrid has an Objects property, so you don’t need to implement your own workarounds.
I’ve implemented both changes and handled the update for selected cells - see the attachment.
Best regards / Pozdrawiam
paweld

paxnet_be

  • New Member
  • *
  • Posts: 25
Re: Font in StringGrid: cannot set different fonts per cell
« Reply #4 on: April 23, 2026, 11:23:57 am »
Thank you, paweld.

This is what I needed, it works, and best of all, you improved it and I learned a lot more about grids !  :D

paxnet_be
« Last Edit: April 23, 2026, 12:42:19 pm by paxnet_be »
Now I know you never know, but at least I know this. - Jean Gabin

 

TinyPortal © 2005-2018