Text Color in TStringGrid

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
kingbarsa
Posts: 6
Joined: Tue Oct 20, 2020 7:06 am

Text Color in TStringGrid

Post by kingbarsa »

Hi All.

Newbie here.

I asked this question on Stack Overflow and got no responses, so hoping someone here will be able to advise me.

The following code worked perfectly in Borland C++ Ver 5, on an old App for about 15 years, but since recompiling the App in C++ Rad Studio (Embarcadero), it has absolutely no effect in influencing the colors of the text contained within the cells of a TStringGrid.

Please advise, appreciated

Essentially StringGrid1->Canvas->Font->Color = clRed; has no effect, yet worked before.

Example:

Code: Select all

void __fastcall TPagesDlgLoadEditorTanks::StringGrid1DrawCell(TObject *Sender,
  int ACol, int ARow, TRect &Rect, TGridDrawState State) {

  if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->IntactStatus == false))
    StringGrid1->Canvas->Font->Color = clRed;
  else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->SelectForUse == true))
    StringGrid1->Canvas->Font->Color = clBlue;
  else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->UseMaxFsm == true))
    StringGrid1->Canvas->Font->Color = clFuchsia;
  else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->UseZeroFsm == true))
    StringGrid1->Canvas->Font->Color = clAqua;
  else
    StringGrid1->Canvas->Font->Color = clBlack;}


Please advise any other method to influence the text colour within the cells of a TStringGrid.

I would also like to know how to color in the background of a cell, but I can keep that for a separate day.

Thank you.
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Text Color in TStringGrid

Post by rlebeau »

kingbarsa wrote: Tue Oct 20, 2020 10:08 am I asked this question on Stack Overflow and got no responses
Link? I don't recall seeing a question like this.
kingbarsa wrote: Tue Oct 20, 2020 10:08 am The following code worked perfectly in Borland C++ Ver 5, on an old App for about 15 years, but since recompiling the App in C++ Rad Studio (Embarcadero), it has absolutely no effect in influencing the colors of the text contained within the cells of a TStringGrid.
Which version did you upgrade to exactly?
kingbarsa wrote: Tue Oct 20, 2020 10:08 am Essentially StringGrid1->Canvas->Font->Color = clRed; has no effect, yet worked before.
You are merely setting the Canvas->Font->Color, but you are not actually drawing the text itself, via Canvas->TextRect() or similar. Do you have the grid's DefaultDrawing property set to true or false? If you want to custom-draw the cells, DefaultDrawing should be false. The fact that this code worked before is more of a fluke than by design. In C++Builder 5, Visual Themes/Styles simply didn't exist yet. Now, they do, so the Grid will honor the currently active theme/style when DefaultDrawing is true.
kingbarsa wrote: Tue Oct 20, 2020 10:08 am Please advise any other method to influence the text colour within the cells of a TStringGrid.
Set the grid's DefaultDrawing property to false, then have your OnDrawCell handler actually draw the cell text as needed.
kingbarsa wrote: Tue Oct 20, 2020 10:08 am I would also like to know how to color in the background of a cell.
Set the grid's DefaultDrawing property to false, then have your OnDrawCell handler set the Canvas->Brush->Color to the desired baackground color, and then call Canvas->FillRect() to fill the provided TRect with that color, before then drawing the text on top of it.
Remy Lebeau (TeamB)
Lebeau Software
kingbarsa
Posts: 6
Joined: Tue Oct 20, 2020 7:06 am

Re: Text Color in TStringGrid

Post by kingbarsa »

Remy

Many thanks for the detailed reply. (As an Engineer and not programmer, it takes us a little longer to pick up the nuances. Thank you for your patience and support)

Yes

Code: Select all

StringGrid1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, StringGrid1->Cells[ACol][ARow]);
Did the trick. As you say, odd that it worked before.

On the background fill question.

I got that to work thanks, but it doesn't fill the entire cell.

If there is no text in the cell, then it fills the bottom half, if there is text, it fills to the top if the text, but there is still a small gap at the top.

See screen shot.

Many thanks, appreciated.

Code for the Fill

Code: Select all

void __fastcall TPagesDlgLoadEditorTanks::StringGrid1DrawCell(TObject *Sender,
	int ACol, int ARow, TRect &Rect, TGridDrawState State) {

	if (ARow < 2)
	  StringGrid1->Canvas->Font->Style = Canvas->Font->Style>>fsBold;

	if (ARow < 2)
	{
	   StringGrid1->Canvas->Font->Color = clBlack;
	   StringGrid1->Canvas->Brush->Color = clLtGray;
	   StringGrid1->Canvas->FillRect(Rect);
	}
	else if (ARow >= 2)
	{
	   if ((ACol < 3) | (ACol > 6))
	   {
		  StringGrid1->Canvas->Font->Color = clBlack;
		  StringGrid1->Canvas->Brush->Color = clWebLavender;
		  StringGrid1->Canvas->FillRect(Rect);
	   }
	}

	if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->IntactStatus == false))
		StringGrid1->Canvas->Font->Color = clRed;
	else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->SelectForUse == true))
		StringGrid1->Canvas->Font->Color = clBlue;
	else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->UseMaxFsm == true))
		StringGrid1->Canvas->Font->Color = clFuchsia;
	else if ((ARow >= 2) & (LiquidLoads->TankHeaderArray[0][ARow - 2]->UseZeroFsm == true))
		StringGrid1->Canvas->Font->Color = clAqua;
	else
		StringGrid1->Canvas->Font->Color = clBlack;

		StringGrid1->Canvas->TextRect(Rect, Rect.Left, Rect.Top, StringGrid1->Cells[ACol][ARow]);  //THE MISSING LINE BEFORE
}
While I have the code included, I could not get the BOLD to work. That line was not my only attempt, just the most recent attempt. (It is less important, but would be nice to have)
Attachments
Capture01.PNG
Capture01.PNG (43.86 KiB) Viewed 781 times
rlebeau
BCBJ Author
BCBJ Author
Posts: 1726
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Text Color in TStringGrid

Post by rlebeau »

If the event is not giving you correct TRect coordinates, then that is a bug in the Grid itself. Do you have the same problem if you use the grid's CellRect() method?

Regarding the rest of the code, I see a few issues:

When ARow is >= 2, you need to use the "||" LOGICAL OR operator instead of the "|" BITWISE OR operator when checking the ACol values.

When ARow is >= 2 and ACol is 3..6, you are not setting the Canvas->Brush or calling Canvas->FillRect(). When DefaultDrawing is false, you have to draw *everything*.

I would rewrite the code more like this:

Code: Select all

void __fastcall TPagesDlgLoadEditorTanks::StringGrid1DrawCell(TObject *Sender,
	int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
	TRect R = Rect;
	// if needed...
	// R = StringGrid1->CellRect(ACol, ARow);

	if (ARow < 2)
	{
		StringGrid1->Canvas->Brush->Color = clLtGray;
		StringGrid1->Canvas->Font->Color = clBlack;
		StringGrid1->Canvas->Font->Style = Canvas->Font->Style >> fsBold;
	}		
	else
	{
		if ((ACol < 3) || (ACol > 6))
		{
			StringGrid1->Canvas->Brush->Color = clWebLavender;
			StringGrid1->Canvas->Font->Color = clBlack;
		}
		else
		{
			StringGrid1->Canvas->Brush->Color = StringGrid1->Color;
			StringGrid1->Canvas->Font->Color = StringGrid1->Font->Color;
		}

		auto *Header = LiquidLoads->TankHeaderArray[0][ARow - 2]; // <-- replace auto if needed...

		if (!Header->IntactStatus)
			StringGrid1->Canvas->Font->Color = clRed;
		else if (Header->SelectForUse)
			StringGrid1->Canvas->Font->Color = clBlue;
		else if (Header->UseMaxFsm)
			StringGrid1->Canvas->Font->Color = clFuchsia;
		else if (Header->UseZeroFsm)
			StringGrid1->Canvas->Font->Color = clAqua;
		else
			StringGrid1->Canvas->Font->Color = clBlack;

		// set StringGrid1->Canvas->Font->Style as needed...
	}

	StringGrid1->Canvas->FillRect(R);
	StringGrid1->Canvas->TextRect(R, R.Left + 2, R.Top + 2, StringGrid1->Cells[ACol][ARow]);
}
Remy Lebeau (TeamB)
Lebeau Software
kingbarsa
Posts: 6
Joined: Tue Oct 20, 2020 7:06 am

Re: Text Color in TStringGrid

Post by kingbarsa »

Many thanks Remy

Code: Select all

R = StringGrid1->CellRect(ACol, ARow);
was necessary (as well as your other code above), and now it shades the cells in properly. Screen shot attached.

Most appreciated.

Closed.
Attachments
Capture02.PNG
Capture02.PNG (37.36 KiB) Viewed 729 times
Post Reply