Recent

Author Topic: Stringgrid check only one checkbox in a Column  (Read 1452 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Stringgrid check only one checkbox in a Column
« on: December 08, 2023, 03:35:18 pm »
Hi,

In a StringGrid I have a checkbox column. I want only 1 checkbox to be checked int this column. So if you check 1 and then want to check another checkbox in the same column, this should not be possible (until you have unchecked the first).
where to start with this?

morknot

  • Jr. Member
  • **
  • Posts: 51
  • still learning
Re: Stringgrid check only one checkbox in a Column
« Reply #1 on: December 08, 2023, 04:15:33 pm »
A clumsy way but may help..
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1CheckboxToggled(sender: TObject; aCol,
  2.   aRow: Integer; aState: TCheckboxState);
  3. var
  4.   r:integer;
  5.  
  6. begin
  7.  
  8.  For r:=1 to arow-1 do
  9.  StringGrid1.Cells[acol,r]:='0';
  10.  For r:=arow+1 to stringgrid1.RowCount-1 do
  11.  StringGrid1.Cells[acol,r]:='0';
  12.  
  13.  end;      
  14.  
  15.  

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Stringgrid check only one checkbox in a Column
« Reply #2 on: December 08, 2023, 09:17:36 pm »
The approach wasn't good and I'm going to take it differently. In the prepare canvas I check if there are 2 checkmarks in the same column and then color the row red and then turn off the save button. That is workable for me.

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Stringgrid check only one checkbox in a Column
« Reply #3 on: December 08, 2023, 11:59:19 pm »
Use the "OnCheckBoxState" event.

All rows will report, you just uncheck the other rows you don't want checked.
The only true wisdom is knowing you know nothing

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Stringgrid check only one checkbox in a Column
« Reply #4 on: December 09, 2023, 05:21:49 pm »
I don't understand it at all... See example. 3 string grids, each with a checkbox column.
What I want is that if you put 1 check mark in 1 checkbox, the cell will turn yellow (works). If you check 2 cells, they should turn red. I can only do that within 1 stringgrid. However, I want only 1 check mark to be enabled in the 3 string grids together. So if a checkbox is checked in 1 stringgrid and you check a checkbox in another stringgrid, both checkboxes should turn red. And if you uncheck 1, the only remaining one should be yellow again.

Is that possible?

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: Stringgrid check only one checkbox in a Column
« Reply #5 on: December 10, 2023, 12:30:35 am »
Can't you iterate over all three grids in the OnCheckboxToggled handler? The event's Sender tells you the grid, in which the click occured, and your CheckedCells array contains information in which one of the three grids the checked cells are located.

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Stringgrid check only one checkbox in a Column
« Reply #6 on: December 10, 2023, 10:11:20 am »
How can I adjust the color from OnCheckboxToggled ? I can't see that.

I've got it. The code is still very ugly, but it already works.
@wp, you put me on the right track.

« Last Edit: December 10, 2023, 11:37:47 am by Hansvb »

Hansvb

  • Hero Member
  • *****
  • Posts: 715
Re: Stringgrid check only one checkbox in a Column
« Reply #7 on: December 11, 2023, 09:19:33 pm »
It has improved a bit. This will be my practice project to understand stringgrid preparecanvas. i Slowly beginning to understand it.



 

TinyPortal © 2005-2018