Lazarus

Programming => General => Topic started by: Jvan on August 02, 2020, 12:41:56 am

Title: StringGrid: How to color a row?
Post by: Jvan on August 02, 2020, 12:41:56 am
I want to color some rows.

Thanks
Title: Re: StringGrid: How to color a row?
Post by: RayoGlauco 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.
Title: Re: StringGrid: How to color a row?
Post by: Jvan 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.
Title: Re: StringGrid: How to color a row?
Post by: RayoGlauco 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.
Title: Re: StringGrid: How to color a row?
Post by: Jvan 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;".
Title: Re: StringGrid: How to color a row?
Post by: Handoko 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.
Title: Re: StringGrid: How to color a row?
Post by: RayoGlauco 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;
Title: Re: StringGrid: How to color a row?
Post by: Jvan on August 02, 2020, 08:24:09 pm
It works, thanks!
TinyPortal © 2005-2018