Board index » cppbuilder » Change font colour of cell in TStringGrid

Change font colour of cell in TStringGrid

Greetings

I create a TStringGrid and populate it with strings. I want to go in and
change the font colour of selected cells while leaving the other cells in
the default font colour. Is this possible, or do I need to use a different
control?

Thanks for any insights.

Doug Bath

 

Re:Change font colour of cell in TStringGrid


Quote
> I create a TStringGrid and populate it with strings. I want to go in and
> change the font colour of selected cells while leaving the other cells in
> the default font colour. Is this possible, or do I need to use a different
> control?

Look at OnDrawCell event

Regards

Re:Change font colour of cell in TStringGrid


You need to use the OnDrawCell event to manipulate the grid's Canvas.
Essentially, you'll have to draw all of the cells yourself in order to
support custom coloring.

Gambit

Quote
"Doug Bath" <mrsmodernshop...@hotmail.com> wrote in message

news:3d8f38d6$1@newsgroups.borland.com...
Quote
> I create a TStringGrid and populate it with strings. I want
> to go in and change the font colour of selected cells while
> leaving the other cells in the default font colour. Is this possible,
> or do I need to use a different control?

Re:Change font colour of cell in TStringGrid


Thanks Paolo

That worked to a certain extent. Using the following code as a test

    if(ACol==2 && !(ARow%10))
        ReportGrid->Font->Color = clRed;
    else
        ReportGrid->Font->Color = clBlack;

Every 10th cell in column 3 has a red font. Obviously I will do something
more sophisticated in my actual application. The problem is the grid doesn't
repaint unless I force it to and then it seems to be continually repainting
as the text is very jittery.

It looks like you have pointed me in the right direction but I am still not
quite there.

Thanks for your help.

Doug Bath

Re:Change font colour of cell in TStringGrid


You need to actually draw the text at the time you set the color.  You can't
just set the color and leave the rendering to default handling - there isn't
any - you've overriden it when you assigned the OnDrawCell handler.  You
also need to modify the Font of the grid's Canvas, not the grid itself.

    void __fastcall TForm1:ReportGridDrawCell(TObject* Sender, int ACol, int
ARow, TRect Rect, TGridDrawState State)
    {
        if( State.Contains(gsSelected) )
        {
            ReportGrid->Canvas->Brush->Color = clHighlight;
            ReportGrid->Canvas->Font->Color = clHighlightText;
        }
        else
        {
            ReportGrid->Canvas->Brush->Color = ReportGrid->Color;

            if( (ACol==2) && ((ARow%10)==0) )
                ReportGrid->Canvas->Font->Color = clRed;
            else
                ReportGrid->Canvas->Font->Color = Report1->Font->Color;
        }

        ReportGrid->Canvas->FillRect(Rect);
        ReportGrid->Canvas->TextRect(Rect, Rect.Left, Rect.Top,
ReportGrid->Cells[ACol][ARow]);
    }

Gambit

Quote
"Doug Bath" <mrsmodernshop...@hotmail.com> wrote in message

news:3d8f4b8e$1@newsgroups.borland.com...
Quote
> Obviously I will do something more sophisticated in my
> actual application. The problem is the grid doesn't repaint
> unless I force it to and then it seems to be continually
> repainting as the text is very jittery.

Other Threads