Recent

Author Topic: StringGrid: How to color a row?  (Read 385 times)

Jvan

  • New Member
  • *
  • Posts: 46
StringGrid: How to color a row?
« on: August 02, 2020, 12:41:56 am »
I want to color some rows.

Thanks

RayoGlauco

  • Full Member
  • ***
  • Posts: 106
  • Beers: 1567
Re: StringGrid: How to color a row?
« Reply #1 on: August 02, 2020, 12:52:20 am »
I found this example, using OnPrepareCanvas:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.gridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. begin
  4.   if (gdFixed in aState) then
  5.     Exit;
  6.   if (aRow=grid.Row) then
  7.     if (aCol=grid.Col) then
  8.       grid.Canvas.Brush.Color:=clYellow
  9.     else grid.Canvas.Brush.Color:=clBlue;
  10. end;
You can adapt it.
To err is human, but to really mess things up, you need a computer.

Jvan

  • New Member
  • *
  • Posts: 46
Re: StringGrid: How to color a row?
« Reply #2 on: August 02, 2020, 01:50:35 am »
I found this example, using OnPrepareCanvas:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.gridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. begin
  4.   if (gdFixed in aState) then
  5.     Exit;
  6.   if (aRow=grid.Row) then
  7.     if (aCol=grid.Col) then
  8.       grid.Canvas.Brush.Color:=clYellow
  9.     else grid.Canvas.Brush.Color:=clBlue;
  10. end;
You can adapt it.

That code works when I select a row, but I need to color rows by other ways, for instance when clicking a button.

RayoGlauco

  • Full Member
  • ***
  • Posts: 106
  • Beers: 1567
Re: StringGrid: How to color a row?
« Reply #3 on: August 02, 2020, 02:34:30 am »
Well, maybe you can keep a list of cells that must have different colors and apply these colors in the OnPrepareCanvas event.
To err is human, but to really mess things up, you need a computer.

Jvan

  • New Member
  • *
  • Posts: 46
Re: StringGrid: How to color a row?
« Reply #4 on: August 02, 2020, 02:51:25 am »
Well, maybe you can keep a list of cells that must have different colors and apply these colors in the OnPrepareCanvas event.

I thought that, but the question would be how to color a specific cell. Something like "MyGrid.Cells[x, y].Color := clBlue;".

Handoko

  • Hero Member
  • *****
  • Posts: 3658
  • My goal: build my own game engine using Lazarus
Re: StringGrid: How to color a row?
« Reply #5 on: August 02, 2020, 06:15:45 am »
There are some demos showing how to color certain cells of a StringGrid:
https://wiki.freepascal.org/Portal:HowTo_Demos

Visit the link above, scroll down until User Interface section then you will find Searchable StringGrid.

RayoGlauco

  • Full Member
  • ***
  • Posts: 106
  • Beers: 1567
Re: StringGrid: How to color a row?
« Reply #6 on: August 02, 2020, 08:57:12 am »
This is a piece of the code suggested by Handoko. You have a list of cells that you want to color and you apply the color in the OnPrepareCanvas event. See the complete example.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. var
  4.   i: Integer;
  5. begin
  6.  
  7.   if not(sender is TStringGrid) then Exit;
  8.  
  9.   // Paint the background yellow for the results
  10.   for i := Low(Results) to High(Results) do
  11.     if (Results[i].Col = aCol) and (Results[i].Row = aRow) then
  12.       (sender as TStringGrid).Canvas.Brush.Color := clYellow;
  13.  
  14. end;
To err is human, but to really mess things up, you need a computer.

Jvan

  • New Member
  • *
  • Posts: 46
Re: StringGrid: How to color a row?
« Reply #7 on: August 02, 2020, 08:24:09 pm »
It works, thanks!

 

TinyPortal © 2005-2018