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:
private
fCBRow : integer;
In
TForm1.Create, I shrink the fixed column, initialize
fCBRow to -1 and clear all the checkboxes.
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.
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?