Recent

Author Topic: Is there some grid with color property for each cell of the grid ?  (Read 3382 times)

pasc92

  • Newbie
  • Posts: 3
Hello,

I'm a newbee in Lazarus practice and I have some troubles with some "missing" documentation...

I would like to know if there is a Lazarus grid component with color property for each cell of the grid.

I precise that I do not want just paint with canvas a cell or few cells, but choice by coding the color of each cell.
That's for very, very simple Conway's Life game, as an example to learn Lazarus.

Many thanks for your answers.

Pascal

P.S. Sorry for my poor english.

wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Is there some grid with color property for each cell of the grid ?
« Reply #1 on: March 21, 2013, 09:26:55 am »
You can use the standard TStringGrid and add a handler to the event OnPrepareCanvas. In this event you can change the color of each cell, e.g. (untested...)

Code: [Select]
procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
begin
  if odd(ACol) and odd(ARow) then
    StringGrid1.Canvas.Brush.Color := clRed
  else if odd(ACol) then
    StringGrid1.Canvas.Brush.Color := clSilver
  else if odd(ARow) then
    StringGrid1.Canvas.Brush.Color := clYellow
end;

pasc92

  • Newbie
  • Posts: 3
Re: Is there some grid with color property for each cell of the grid ?
« Reply #2 on: March 21, 2013, 01:41:11 pm »
Hello wp,

Many thanks for your answer.

I have read a tutorial about StringGrid who describe this way of coding, and I try it.
Each column or row or a unique cell can be painting with canvas.

But this not permits to "store" the color of each cell (independently), like an array of colorised cells.

Best regards.
Pascal


wp

  • Hero Member
  • *****
  • Posts: 13630
Re: Is there some grid with color property for each cell of the grid ?
« Reply #3 on: March 21, 2013, 02:45:25 pm »
But you can always store the colors in a separate 2D array that you use like this:
Code: [Select]
var
  CellColors: array of TColor;
//...

  // call this after the cell count of the grid is known.
  SetLength(CellColors, StringGrid1.ColCount, StringGrid1.RowCount);
  CellColors[0,0] := clRed;
  CellColors[0,1] := clBlue;
  // etc.

procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
begin
  StringGrid1.Canvas.Brush.Color := CellColors[aCol, aRow];
end;
« Last Edit: March 25, 2013, 11:46:31 pm by wp »

pasc92

  • Newbie
  • Posts: 3
Re: Is there some grid with color property for each cell of the grid ?
« Reply #4 on: March 25, 2013, 10:48:09 pm »
Hello wp,

I try it... Very nice event "OnPrepareCanvas" !!

I can progress in TStringGrid usage with your help.

Thanks a lot to you.

Pascal

 

TinyPortal © 2005-2018