Recent

Author Topic: [SOLVED] Change stringgrid row font color  (Read 10812 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
[SOLVED] Change stringgrid row font color
« on: August 20, 2017, 07:43:58 pm »
Hi there,

I have a stringgrid in which the first non-fixed column is a checkbox column created as follows
Code: Pascal  [Select][+][-]
  1.     StringGrid1.Columns.Add;
  2.     StringGrid1.Columns[0].ButtonStyle := cbsCheckboxColumn;
  3.     StringGrid1.Columns[0].Title.Caption := {Space(6)} 'Oui/Non';
  4.  

I'm trying to change/toggle the color of the text in the stringgrid row when the checkbox on a row is toggled. I tried the code below but it doesn't work for me.

Code: Pascal  [Select][+][-]
  1. procedure TfrmDonnees.StringGrid1CheckboxToggled(sender: TObject;
  2.   aCol, aRow: Integer; aState: TCheckboxState);
  3. begin
  4.   if aState = cbChecked then
  5.     StringGrid1.Canvas.Font.Color :=  clRed            // Red font
  6.   else
  7.     StringGrid1.Canvas.Font.Color :=  clBlack;         // Black font
  8. end;
  9.  

What am I doing wrong?

JD
« Last Edit: August 20, 2017, 08:23:43 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Change stringgrid row font color
« Reply #1 on: August 20, 2017, 07:52:32 pm »
I think you should not put the drawing related things inside StringGrid1CheckboxToggled. You can see how to customize StringGrid color on my code:
http://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.html#msg249361

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Change stringgrid row font color
« Reply #2 on: August 20, 2017, 08:00:56 pm »
Columns[?].Color := ?

 I don't think you'll get what you want, still after this .
 most likely will change color for the whole column .
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Change stringgrid row font color
« Reply #3 on: August 20, 2017, 08:04:26 pm »
Sorry, I noticed after that you said ROW..

 For Special effects in the cell, use OwnerStyle of drawing and process the OnDrawCell
event.
The only true wisdom is knowing you know nothing

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Change stringgrid row font color
« Reply #4 on: August 20, 2017, 08:23:19 pm »
I think you should not put the drawing related things inside StringGrid1CheckboxToggled. You can see how to customize StringGrid color on my code:
http://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.html#msg249361

Thanks Handoko

This worked

Code: Pascal  [Select][+][-]
  1. procedure TfrmDonnees.StringGrid1PrepareCanvas(sender: TObject;
  2.   aCol, aRow: Integer; aState: TGridDrawState);
  3. begin
  4.   if (Sender as TStringGrid).ColCount > 1 then
  5.   begin
  6.     if (Sender as TStringGrid).Cells[1, aRow] = '1' then
  7.       (Sender as TStringGrid).Canvas.Font.Color :=  clRed
  8.     else
  9.       (Sender as TStringGrid).Canvas.Font.Color :=  clBlack;
  10.   end;
  11.  

By the way, I loved your example.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Bitbeisser

  • New Member
  • *
  • Posts: 28
Re: Change stringgrid row font color
« Reply #5 on: August 10, 2022, 12:00:57 am »
This worked

Code: Pascal  [Select][+][-]
  1. procedure TfrmDonnees.StringGrid1PrepareCanvas(sender: TObject;
  2.   aCol, aRow: Integer; aState: TGridDrawState);
  3. begin
  4.   if (Sender as TStringGrid).ColCount > 1 then
  5.   begin
  6.     if (Sender as TStringGrid).Cells[1, aRow] = '1' then
  7.       (Sender as TStringGrid).Canvas.Font.Color :=  clRed
  8.     else
  9.       (Sender as TStringGrid).Canvas.Font.Color :=  clBlack;
  10.   end;
  11.  
Well, I tried the same thing, changing the color of the text of a tStringGrid row depending on the statis of the checkbox when toggled, but it doesn't seem to do anything for me. What could I possibly be missing here? :( :-\

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Change stringgrid row font color
« Reply #6 on: August 10, 2022, 12:44:15 am »
Well, I tried the same thing, changing the color of the text of a tStringGrid row depending on the statis of the checkbox when toggled, but it doesn't seem to do anything for me. What could I possibly be missing here? :( :-\
Where is column locate for checkbox ? is column 1 ? or is other column ?

Bitbeisser

  • New Member
  • *
  • Posts: 28
Re: Change stringgrid row font color
« Reply #7 on: August 10, 2022, 01:58:43 am »
Well, I tried the same thing, changing the color of the text of a tStringGrid row depending on the statis of the checkbox when toggled, but it doesn't seem to do anything for me. What could I possibly be missing here? :( :-\
Where is column locate for checkbox ? is column 1 ? or is other column ?
In my case, it's column 0, and yes, I have my code adjusted accordingly to check for the check nark....  :D

Bitbeisser

  • New Member
  • *
  • Posts: 28
Re: [SOLVED] Change stringgrid row font color
« Reply #8 on: August 10, 2022, 05:27:56 am »
 :o :( :-[ Well, never mind. After having dinner, I found my mistake right away. I had written (well, copied and modified) the PrepareCanvas procedure first, then assigned to the StringGrid event in the Object Inspector. Well, to the wrong event, which never got fired... :(
I think i need to make a note only to program on a full stomach...  :-[

 

TinyPortal © 2005-2018