TRzStringGrid set specific cells to a color?

Components on the Raize Lists palette: list boxes, check lists, combo boxes, tree views, list views, grids, etc.

TRzStringGrid set specific cells to a color?

Postby Norm@Home » Mon Jun 04, 2018 7:43 pm

I've done some googling and for either the regular VCL TStringGrid or Raize stringgrid you should be able to change the color of text in a cell in the OnDrawCell event. I've tried the suggested method i.e. to set a global boolean variable to True before the value is assigned to the particular cell:
Code: Select all
If bNeedsColor Then
   Begin
     Canvas.Brush.Color := clRed;
     Canvas.FillRect(Rect);
     Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, sgData.Cells[ACol,ARow]);
   End;


And tracing through the code bNeedsColor is true, then I'm assigning a value to the cell that needs color "sgData.Cells[iX,iY] := Format( '%4d%s', [iCurrent, '-'+#9679] )" but when the grid is fully populated the cells that should have color, don't.

Idea's?
Norm@Home
 
Posts: 12
Joined:
Wed Mar 15, 2017 5:16 pm

Re: TRzStringGrid set specific cells to a color?

Postby Raize Support » Tue Jun 05, 2018 1:48 am

What does your complete OnDrawCell event handler look like? I am concerned that the Canvas property that you are specifying in the code below is the Canvas for the form, and not the Canvas of the string grid.

As such, I think the code should be:

Code: Select all
If bNeedsColor Then
   Begin
     sgData.Canvas.Brush.Color := clRed;
     sgData.Canvas.FillRect(Rect);
     sgData.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, sgData.Cells[ACol,ARow]);
   End;


Ray
Raize Software Support
Raize Software
http://www.raize.com
Raize Support
 
Posts: 616
Joined:
Fri Mar 25, 2011 9:04 pm

Re: TRzStringGrid set specific cells to a color?

Postby Norm@Home » Wed Jun 06, 2018 6:14 pm

Hi Ray:

I'm getting the code from a couple of websites that purport the code I posted to allow you to change the color of the text in the specific cell. The code I posted is the entirety of what was provided (and what was in my OnDrawCell event) and it seems like it was basically wrong.

I see that the code should have been either "sgdata.canvas." or "TRzStringGrid(sender).canvas.", with your suggestion I did manage to get some results but there are two problems:

First, what this code apparently does is not set the text color but the background color of the cell; I want the text to be red rather than black and for the cell background color to not change so it should be white(when using no theme).

Second, once you execute this code all cells are then painted red so apparently on the next cell to be drawn if the boolean bNeedsColor is false you have to change the color back to black (in this wrong instance to the string grids normal background color) otherwise everything from the first cell that needs color to the end of what's written into the grid is red.

So how do you change the text color of the cell and not the background color? I can remedy the second problem just by setting the color back if the bNeedColor is false.

- Norm
Norm@Home
 
Posts: 12
Joined:
Wed Mar 15, 2017 5:16 pm

Re: TRzStringGrid set specific cells to a color?

Postby Raize Support » Sat Jun 09, 2018 1:51 am

Hi Norm,

Here is a sample event handler that shows how to set the color of the text. Notice the use of the Font.Color. Also, notice how the TextRect method is called in all cases. The color settings for the background and font will be set when this event handler is called. You simply change the colors when you need to. In this example, all cells in Column 2 will be shown in Red text.

Code: Select all
procedure TForm18.RzStringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if ACol = 2 then
  begin
     RzStringGrid1.Canvas.Font.Color := clRed;
  end;
  RzStringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, RzStringGrid1.Cells[ACol,ARow]);
end;


If you want to get really fancy, you can look at the State parameter and determine if the current cell is selected and then decide if you want to changed the way a selected cell looks like.

Hope this helps.

Ray
Raize Software Support
Raize Software
http://www.raize.com
Raize Support
 
Posts: 616
Joined:
Fri Mar 25, 2011 9:04 pm

Re: TRzStringGrid set specific cells to a color?

Postby Norm@Home » Fri Jun 15, 2018 9:28 pm

Thanks Ray, that worked great.
Norm@Home
 
Posts: 12
Joined:
Wed Mar 15, 2017 5:16 pm


Return to Lists

Who is online

Users browsing this forum: No registered users and 0 guests

cron