Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to change stringgrid cell color and font programmatically


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Jan 3, 2018 10:13 AM Last Post By: Eitan Arbel Threads: [ Previous | Next ]
Eitan Arbel

Posts: 508
Registered: 2/24/13
How to change stringgrid cell color and font programmatically  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 30, 2017 8:59 AM
i need to change a small TStringGrid's cell color and font programmatically, after all the grid is filled by the user, and checked by the app.

for example :
my grid is 3x3 cells.
if the user fills the cells with predetermined set of specific rules let's say 1x1='A', 1x2='B', 1x3='C', 2x1='D' etc... and everything is fine, then (after the app checks that), the cells should be painted with background=green and font=black.
but if the user made a mistake like 1x1='A', 1x2='9', 1x3='C', 2x1='D' etc... then i need only the cell 1x2 to be painted with background=red, and font=yellow.
the app should not paint the cells while the user is filling the cells, but only after - programmatically.

how can i do that please?
Thanks !
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: How to change stringgrid cell color and font programmatically  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2018 3:14 AM   in response to: Eitan Arbel in response to: Eitan Arbel
anybody...?
Bob Carson

Posts: 62
Registered: 10/8/04
Re: How to change stringgrid cell color and font programmatically  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2018 1:48 PM   in response to: Eitan Arbel in response to: Eitan Arbel
StringGrid1.BeginUpdate;
StringGrid1.Fill.Color := Edit41.FontColor;

for I := 0 to StringGrid1.ColumnCount - 1 do
for J := 0 to StringGrid1.RowCount - 1 do
begin
StringGrid1.FontColors[I,J] := WkFontClr;
StringGrid1.ColumnWidths[I] := 120;
StringGrid1.RowHeights[J] := 30;
end;

StringGrid1.EndUpdate;
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: How to change stringgrid cell color and font programmatically  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 3, 2018 2:02 AM   in response to: Bob Carson in response to: Bob Carson
Thank you very much for your reply Bob, but it seems like Delphi (and i) don't recognize many of the words here :
StringGrid1.Fill...
FontColors
ColumnWidths
RowHeights

do i need to add something to the Uses ?

i use Delphi Berlin if it helps

Thanks !
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: How to change stringgrid cell color and font programmatically
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 3, 2018 7:05 AM   in response to: Eitan Arbel in response to: Eitan Arbel
Eitan Arbel wrote:

Thank you very much for your reply Bob, but it seems like Delphi (and
i) don't recognize many of the words here : StringGrid1.Fill...
FontColors
ColumnWidths
RowHeights

do i need to add something to the Uses ?

i use Delphi Berlin if it helps

Thanks !

You can change the background in the OnDrawColumnCell event (like in a
VCL application). Here is a quick example:

procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State:
TGridDrawStates);
begin
  if Odd(row) then
    Canvas.Fill.Color:=  TAlphaColorRec.Red
  else
    Canvas.Fill.Color:=  TAlphaColorRec.Green;
 Canvas.FillRect(bounds, 0, 0, AllCorners, 0.5);
end;
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: How to change stringgrid cell color and font programmatically  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 3, 2018 10:13 AM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Thanks Lagos, i already knew that, but i was hoping to change the background and font without using the OnDrawColumnCell event.

the solution i came up with, is by setting a global variable in my app:
IsFinished=True to set the colors, and IsFinished=False to do nothing (not set the colors)

then in some procedures i check if the grid is full (finished)
and in OnDrawColumnCell i check if IsFinished then change the colors...
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02