Solved

Color of text in cells of a TStringGrid

Posted on 2015-02-12
3
114 Views
Last Modified: 2015-02-12
Depending on some condition, I want to have text in specific TStringGrid cells be (for example) written in RED.
So I have to code in the DrawCell event
But I am not sure of which property of which object I have to set to clRed
To write the text, I code this :
Canvas.TextOut(Rect.Left, Rect.Top,SG.Cells[ACol,ARow]);
But what shall I set to clRed ?
0
Comment
Question by:LeTay
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 250 total points
ID: 40605334
StringGrid.canvas.Brush.Color := clRed;
0
 
LVL 24

Expert Comment

by:jimyX
ID: 40605347
Brush sets the cell. But, if you want to set only the font:
StringGrid.canvas.Font.Color:= clRed;
0
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 250 total points
ID: 40605352
This is example how to use OnCellDraw event in stringgrid:
procedure TForm1.MyStrGridDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  s : string;
begin
  inherited;

  try
    if (ACol > 0) and (ARow > 0) then
    begin
      s := MyStrGrid.Cells[ACol, ARow];

      if gdSelected in State then
      begin
        MyStrGrid.Canvas.Brush.Color := clHighlight;
        MyStrGrid.Canvas.Font.Color := clHighlightText;
        MyStrGrid.Canvas.Font.Style := [fsBold];
      end
      else
      begin
        if s = 'some text' then
          MyStrGrid.Canvas.Font.Color := clRed
        else
          MyStrGrid.Canvas.Font.Color := clWindowText;

        MyStrGrid.Canvas.Font.Style := [];
      end;

      MyStrGrid.Canvas.Brush.Style := bsSolid;
      MyStrGrid.Canvas.FillRect(Rect);
      MyStrGrid.Canvas.TextOut(Rect.Left, Rect.Top, s);
    end;
  except
  end;
end;

Open in new window


... this example check if 'some text' is in cell and draw it in red.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now