[SOLVED] I'm stumped: Enforcing a unique checkbox in TStringGrid lazarus / 31-LCL ---------------------------------------------------------------------- EganSolo · 2025-08-07 01:16 Sample Project attached. //ADDENDUM: I've managed to solve this issue (see my latest reply). The attached project contains the attempt I describe here commented out (in case you'd like to see for yourself) and the actual solution. // I want to enforce no more than one checkbox being checked in a TStringGrid, where the column's button style is set to cbsCheckBoxColumn. If a box is already checked and the user clicks on another cell, then the checked box must be unchecked. I've checked that the column with the checkboxes is not read-only. The Grid's options goAlwaysShowEditor and goEditing are set to true. I've declared a private attribute in TForm1 as follows: 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   private  fCBRow : integer;  In TForm1.Create, I shrink the fixed column, initialize fCBRow to -1 and clear all the checkboxes. 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.FormCreate(Sender: TObject);var aRow: integer;begin  StringGrid1.ColWidths[0] := 30;  fCBRow := -1;  with StringGrid1 do  If RowCount > 1    then for aRow := 1 to RowCount -1 do      Cells[cb_col_idx,aRow] := '0';end;  Lastly, I've implemented StringGrid1.OnClick to enforce the restrictions. 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.StringGrid1Click(Sender: TObject);begin  with StringGrid1 do  begin   If RowCount < 3 then exit;   OnClick := Nil;   If (Col = cb_col_idx)   then begin        If Row = fCBRow        then begin        Cells[Col,Row] := '0';          InvalidateCell(Col,Row);          fCBRow  := -1 ;        end        else begin          If fCBRow > -1        then begin                Cells[Col,fCBRow] := '0';              InvalidateCell(Col,Row);              end;          Cells[Col,Row] := '1';          InvalidateCell(Col,Row);          fCBRow := Row;      end;        Invalidate;        Repaint;   end;   OnClick := @StringGrid1Click;  end;end;  But something is wrong: when I click on a cell containing a checkbox, the cell is focused, and the checkbox doesn't change. The OnClick event is invoked and the values are correctly updated, but the grid's visual appearance doesn't change. If I comment out the code in the OnClick event handler, the checkbox behaves correctly. Suggestions? ---------------------------------------------------------------------- jamie · 2025-08-07 01:31 I didn't download your project but I can only take a guess that maybe you could try calling the REPAINT in that event.  Or even Invalidate for the grid. Jamie ---------------------------------------------------------------------- EganSolo · 2025-08-07 03:59 Hi Jamie,   Yeah, that's what I thought too. I've modified the program and the listed code to include InvalidateCell, Invalidate and Repaint. I know it's overkill, but no dice -- the checked box is cleared but the new checked box is not updated.   I'm starting to suspect a bug in TStringGrid. ---------------------------------------------------------------------- EganSolo · 2025-08-07 04:20 Well, I've managed to solve it by a different implementation, which may have been the right way to do it all along. At this point, I'm unsure whether my initial implementation was misusing the OnClick event or if there's a bug lurking somewhere in TStringGrid. Be that as it may, here's how I've managed to make it work: 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 //Implementation of the OnSetCheckBoxState. Once more, fCBRow is an attribute that tracks the currently checked row. //If no row is checked, it's set to -1. procedure TForm1.StringGrid1SetCheckboxState(Sender: TObject; ACol, ARow: Integer; const Value: TCheckboxState);begin  with StringGrid1 do  begin    //Are we setting the value of the currently checked box? If so, uncheck it.    If aRow = fcbRow    then begin      Cells[aCol,aRow] := '0';      fcbRow := -1;    end    else begin      If fCBRow > -1      then Cells[aCol,fCBRow] := '0';      Cells[aCol,aRow] := '1';      fcbRow := aRow;    end;  end;end;