rperies
asked on
Changing the colour of CELLS/TEXT in a stringgrid at Runtime...
I have a StringGrid that is linked to a two dimensional array of records and I am trying to change the colour of the text in a stringgrid in order to correspond to a boolean variable field in the array. If the value is false, then the text should be green, otherwise the text should be red (If the value of the field is true). On startup, the text in the cells within the stringgrid is black. On formactivate, I want them to be green, and then, whenever the array is updated, I want them to turn red (when the value in the boolean field, lets call it BOOKED, becomes true)
If the colour of the CELLS, rather than the TEXT can be changed, then I wouldn't mind that either. In fact, I would prefer it.
Thanks
rperies
If the colour of the CELLS, rather than the TEXT can be changed, then I wouldn't mind that either. In fact, I would prefer it.
Thanks
rperies
Did I sent the answer or not?
if you wish to change the font color use nect tip :
procedure TForm1.StringGrid1DrawCell (Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
StringGrid1.Canvas.FillRec t(Rect);
if ARow = 1 then {Use ACol for column or use both for a cell}
begin
StringGrid1.Canvas.Font.Co lor := clBlue;
StringGrid1.Canvas.Font.Na me := 'Tahoma' ;
StringGrid1.Canvas.Font.St yle := StringGrid1.Canvas.Font.St yle + [fsBold];
end;
DrawText(StringGrid1.Canva s.Handle, PChar(StringGrid1.Cells[AC ol, ARow]), -1,
Rect, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
procedure TForm1.StringGrid1DrawCell
Rect: TRect; State: TGridDrawState);
begin
StringGrid1.Canvas.FillRec
if ARow = 1 then {Use ACol for column or use both for a cell}
begin
StringGrid1.Canvas.Font.Co
StringGrid1.Canvas.Font.Na
StringGrid1.Canvas.Font.St
end;
DrawText(StringGrid1.Canva
Rect, DT_SINGLELINE or DT_VCENTER or DT_LEFT);
end;
For cell color use StringGrid1.Canvas.Brush.C olor := clBlue;
HI,
I don?t know why but my answer
Wasn?t sent.
So I try to answer the question as a comment.
The solution of the problem has the following plan:
1.Create a new component from TStringGrid
(let it call TPSIStringGrid).
2.Declare a new TEvent ? TPSIDrawCell.
3.Declare a field FOnPSIDrawCell of type
TPSIDrawCell.
4.Declare a property OnPSIDrawCell which reads
FOnPSIDrawCell and writes FOnPSIDrawCell.
5.Declare a procedure PSIDrawCell which checks
if FOnPSIDrawCell<>NIL (Assigned).
6.Override the procedure DrawCell in such way:
first execute PSIDrawCell then inherited.
7.Install the TPSIStringGrid on the Pallette.
That?s all.
Now you can change Font and Brush Colors in
OnPSIDrawCell whatever the condition you want.
Sincerely,
Nestorua.
I don?t know why but my answer
Wasn?t sent.
So I try to answer the question as a comment.
The solution of the problem has the following plan:
1.Create a new component from TStringGrid
(let it call TPSIStringGrid).
2.Declare a new TEvent ? TPSIDrawCell.
3.Declare a field FOnPSIDrawCell of type
TPSIDrawCell.
4.Declare a property OnPSIDrawCell which reads
FOnPSIDrawCell and writes FOnPSIDrawCell.
5.Declare a procedure PSIDrawCell which checks
if FOnPSIDrawCell<>NIL (Assigned).
6.Override the procedure DrawCell in such way:
first execute PSIDrawCell then inherited.
7.Install the TPSIStringGrid on the Pallette.
That?s all.
Now you can change Font and Brush Colors in
OnPSIDrawCell whatever the condition you want.
Sincerely,
Nestorua.
ASKER
Sorry, I appreciate the effort but unfortunately this answer seems far too complicated.
ASKER
Ginsonic - I found that your code worked initially, but I can't assign it to a different procedure, such as a buttonclick. The stringgrid should continue to change colour after the record is updated. Can it be done independently of DrawCell?
rperies
rperies
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This sample show how can refresh the cell at update.
HI, rperies,
What a step of my answer is too complicated for you.
I think you asked not only to know the answer but to know
how to solve problems of the kind and to teach youself
new things.
Sincerely,
Nestorua.
What a step of my answer is too complicated for you.
I think you asked not only to know the answer but to know
how to solve problems of the kind and to teach youself
new things.
Sincerely,
Nestorua.
Sorry, but I don't know if I sent an answer.
Sincerely,
Nestorua.