Recent

Author Topic: TStringGrid dynamic number of checkbox columns  (Read 956 times)

_N_

  • New Member
  • *
  • Posts: 33
TStringGrid dynamic number of checkbox columns
« on: February 28, 2021, 07:02:56 pm »
Hello all.
I need to create a grid with many checkboxes. To solve the problem I decided to use TStringGrid component as it has ability to have a column of checkboxes. But to be able to set a column of a string grid as column with checkboxes, it is required to set ButtonStyle property of the column to cbsCheckboxColumn and seems that this is possible only via Columns field of the string grid (so I cannot just set Row/ColCount property, because it doesn't work with/update Columns field).
Everything works fine when I add certain number of columns manually using form editor, but the problem is that I need to add the columns in a loop at runtime. And here I don't know (and failed to find any info on this) how to manually create a grid column that supports ButtonStyle property with cbsCheckboxColumn value. So, at runtime i have empty AStringGrid.Columns and I want to add an item there, but have no idea of which class it should be and how to add it there correctly. Will the following construction be correct and what descendant of TGridColumn I should use?
Code: Pascal  [Select][+][-]
  1. AStringGrid.Columns.Add(SomeTGridColumnDescendant.Create(...));
Thank you in advance.


jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TStringGrid dynamic number of checkbox columns
« Reply #1 on: February 28, 2021, 07:46:26 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   C:TGridColumn;
  4. begin
  5.   C:= StringGrid1.Columns.Add;
  6.   C.ButtonStyle:=cbsCheckboxColumn;
  7. end;
  8.                                                        
  9.  

Did you time me ?
The only true wisdom is knowing you know nothing

_N_

  • New Member
  • *
  • Posts: 33
Re: TStringGrid dynamic number of checkbox columns
« Reply #2 on: February 28, 2021, 08:05:29 pm »
Thank you very much!

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: TStringGrid dynamic number of checkbox columns
« Reply #3 on: February 28, 2021, 09:24:48 pm »
and btw, I could of done it this way too...
Code: Pascal  [Select][+][-]
  1. StringGrid1.Columns.Add.ButtonStyle:=cbsCheckboxColumn;
  2.  
The only true wisdom is knowing you know nothing

speter

  • Sr. Member
  • ****
  • Posts: 335
Re: TStringGrid dynamic number of checkbox columns
« Reply #4 on: February 28, 2021, 11:40:08 pm »
You could also look at the TCheckGroup component; it might be simpler.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018