Highlighting a StringGrid Cell? lazarus / 31-LCL ---------------------------------------------------------------------- segfault · 2018-10-15 08:36 There are some examples of how to do this on the net, mainly using Delphi, but I can't seem to get it to work for my particular application. What I want to do is change the background/text colour of a cell when the button is clicked (background colour to yellow, text to black). See screenshot below. https://i.postimg.cc/9M7df9Mg/grid.png Here is my code so far : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,  StdCtrls, Types; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    StringGrid1: TStringGrid;    procedure FormCreate(Sender: TObject);    procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;      aRect: TRect; aState: TGridDrawState);  private   public   end; var  Form1: TForm1; implementationconst  N : array[0..10,0..10] of byte =  ((0,1,2,3,4,4,3,2,1,1,0),   (1,0,1,3,4,4,4,3,2,1,0),   (2,1,0,2,3,4,5,4,3,2,0),   (3,3,2,0,2,4,5,6,5,3,1),   (4,4,3,2,0,2,5,7,7,5,2),   (4,4,4,4,2,0,3,7,9,8,4),   (3,4,5,5,5,3,0,5,9,11,8),   (2,3,4,6,7,7,5,0,8,15,15),   (1,2,3,5,7,9,9,8,0,15,30),   (1,1,2,3,5,8,11,15,15,0,61),   (0,0,0,1,2,4,8,15,30,61,0)); {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i, j : byte;begin  // load numbers into grid  for i := 0 to 10 do    for j := 0 to 10 do      StringGrid1.Cells[i,j] := IntToStr(N[i,j]);end; procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);var  r, c : byte;begin  // set background/font colours  for r := 0 to 10 do    for c := r + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin          Canvas.Brush.Color:= clRed;        canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);        Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);      end;  for c := 0 to 10 do    for r := c + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin        Canvas.Brush.Color:= clBlack;          canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);          Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);        end;end; end.                  There seem to be several ways to do what I want but I don't understand very well how any of them work. For example, what is that fires the DrawCell procedure? and since the basic colours don't change apart from when a cell is highlighted, shouldn't initializing the colours  be in the FormCreate procedure? but I'm not sure how to do that. I've had a look at the StringGrid reference on the Wiki but it's just confusing me. To be honest I think I'm having these problems because I've never really understand the basic object model which underlies how the GUI works, and I've never found a good explanation of it. Anyway, thanks in advance for any help. ---------------------------------------------------------------------- segfault · 2018-10-15 09:22 Adding an onSelectCell event works but only for the cells which haven't been painted (the middle diagonal). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,  StdCtrls, Types; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    StringGrid1: TStringGrid;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;      aRect: TRect; aState: TGridDrawState);    procedure StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;      var CanSelect: Boolean);  private   public   end; var  Form1: TForm1; implementationconst  N : array[0..10,0..10] of byte =  ((0,1,2,3,4,4,3,2,1,1,0),   (1,0,1,3,4,4,4,3,2,1,0),   (2,1,0,2,3,4,5,4,3,2,0),   (3,3,2,0,2,4,5,6,5,3,1),   (4,4,3,2,0,2,5,7,7,5,2),   (4,4,4,4,2,0,3,7,9,8,4),   (3,4,5,5,5,3,0,5,9,11,8),   (2,3,4,6,7,7,5,0,8,15,15),   (1,2,3,5,7,9,9,8,0,15,30),   (1,1,2,3,5,8,11,15,15,0,61),   (0,0,0,1,2,4,8,15,30,61,0)); {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i, j : byte;begin  // load numbers into grid  for i := 0 to 10 do    for j := 0 to 10 do      StringGrid1.Cells[i,j] := IntToStr(N[i,j]);end; procedure TForm1.Button1Click(Sender: TObject);begin  StringGrid1.Row:=4;  StringGrid1.Col:=4;  StringGrid1.SetFocus;end; procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);var  r, c : byte;begin  // set background/font colours  for r := 0 to 10 do    for c := r + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin          Canvas.Brush.Color:= clRed;        canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);        Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);      end;  for c := 0 to 10 do    for r := c + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin        Canvas.Brush.Color:= clBlack;          canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);          Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);        end;end; procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;  var CanSelect: Boolean);begin  StringGrid1.SelectedColor:= clBlue;  StringGrid1.Options := StringGrid1.Options + [goDrawFocusSelected];end; end. ---------------------------------------------------------------------- paweld · 2018-10-15 09:34 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);var  r, c : byte;begin  // set background/font colours  for r := 0 to 10 do    for c := r + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin          Canvas.Brush.Color:= clRed;        canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);        Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);      end;  for c := 0 to 10 do    for r := c + 1 to 10 do      if (aCol = c) and (aRow = r) then        with TStringGrid(sender) do begin        Canvas.Brush.Color:= clBlack;          canvas.Font.Color:= clWhite;          canvas.FillRect(aRect);          Canvas.TextOut(aRect.Left+2,aRect.Top+2,Cells[aCol, aRow]);        end;  //set select  if (gdSelected in aState) or (gdFocused in aState) then  begin    StringGrid1.Canvas.Brush.Color:=clBlue;    StringGrid1. Canvas.Font.Style:=StringGrid1.Canvas.Font.Style+[fsBold];    StringGrid1.Canvas.FillRect(aRect);    StringGrid1.Canvas.TextOut(aRect.Left+2, aRect.Top+2, StringGrid1.Cells[aCol, aRow]);  end;end; ---------------------------------------------------------------------- segfault · 2018-10-15 10:34 Many thanks paweld.  It works now, and more importantly, I understand why. Easy when you know how. ---------------------------------------------------------------------- wp · 2018-10-15 16:01 I would not use the OnDrawCell event for this purpose. What if the column and row headers of the grid are themed (i.e. Grid.TitleStyle = tsNative), or if there are sorting icons in the column headers? You fully paint the headers - theming will be lost, sorting icons will be gone. What if your grid supports Columns and one column has ButtonStyle = cbsCheckboxColumn, i.e. should display a checkbox in each cell of that column? This code does not paint any checkboxes... What if the cells of the 2nd column will be centered? You can achieve this by setting the Alignment of the corresponding Column accordinging. But your code ignores it - you have to redo all the formattings which have been prepared. The Lazarus way to modifiy the canvas attributes used by the painting process is to hook into the OnPrepareCanvas event. Before Lazarus draws a cell it sets up all canvas properties to the required values in the PrepareCanvas method; at the end of this method it fires the event OnPrepareCanvas which gives the opportunity to modify these properties again. Then the cell is drawn (with all its gimmics: themed header columns, sorting icons, checkboxes, left/center/right alignment etc) using the canvas settings prepared immediately before. So, you only must write an event handler for OnPrepareCanvas to set the color of the selected cell - this color will be used as cell background subsequently and everything of the cell format will be kept. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;  aState: TGridDrawState);begin  if (aCol >= StringGrid1.FixedCols) and (aCol < aRow) then begin    StringGrid1.Canvas.Brush.Color := clBlack;    StringGrid1.Canvas.Font.Color := clWhite;  end else  if (aRow >= StringGrid1.FixedCols) and (aRow < aCol) then begin    StringGrid1.Canvas.Brush.Color := clRed;    StringGrid1.Canvas.Font.Color := clWhite;  end;  if (aCol = StringGrid1.Col) and (aRow = StringGrid1.Row) then begin    StringGrid1.Canvas.Brush.Color := clYellow;     StringGrid1.Canvas.Font.Color := clBlack;  end;end; ---------------------------------------------------------------------- Handoko · 2018-10-16 02:45 I recommend to use OnPrepareCanvas too. I forget what are the reasons but I still remember using OnDrawCell is not good for changing the color of cells. If anyone interested to see the example of using OnPrepareCanvas, you can try this: http://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.ht ml#msg249361 ---------------------------------------------------------------------- balazsszekely · 2018-10-16 03:30 QuoteI forget what are the reasons but I still remember using OnDrawCell is not good for changing the color of cells.@wp already explained why OnPrepareCanvas is a much better approach, but you can also read if you wish the grid reference page: http://wiki.lazarus.fre epascal.org/Grids_Reference_Page#Description_of_grid.27s_drawing_proce ss